]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/ceph/dir.c
SELinux: Use d_is_positive() rather than testing dentry->d_inode
[mirror_ubuntu-bionic-kernel.git] / fs / ceph / dir.c
CommitLineData
3d14c5d2 1#include <linux/ceph/ceph_debug.h>
2817b000
SW
2
3#include <linux/spinlock.h>
4#include <linux/fs_struct.h>
5#include <linux/namei.h>
5a0e3ad6 6#include <linux/slab.h>
2817b000
SW
7#include <linux/sched.h>
8
9#include "super.h"
3d14c5d2 10#include "mds_client.h"
2817b000
SW
11
12/*
13 * Directory operations: readdir, lookup, create, link, unlink,
14 * rename, etc.
15 */
16
17/*
18 * Ceph MDS operations are specified in terms of a base ino and
19 * relative path. Thus, the client can specify an operation on a
20 * specific inode (e.g., a getattr due to fstat(2)), or as a path
21 * relative to, say, the root directory.
22 *
23 * Normally, we limit ourselves to strict inode ops (no path component)
24 * or dentry operations (a single path component relative to an ino). The
25 * exception to this is open_root_dentry(), which will open the mount
26 * point by name.
27 */
28
29const struct inode_operations ceph_dir_iops;
30const struct file_operations ceph_dir_fops;
52dfb8ac 31const struct dentry_operations ceph_dentry_ops;
2817b000
SW
32
33/*
34 * Initialize ceph dentry state.
35 */
36int ceph_init_dentry(struct dentry *dentry)
37{
38 struct ceph_dentry_info *di;
39
40 if (dentry->d_fsdata)
41 return 0;
42
36e21687 43 di = kmem_cache_alloc(ceph_dentry_cachep, GFP_NOFS | __GFP_ZERO);
2817b000
SW
44 if (!di)
45 return -ENOMEM; /* oh well */
46
47 spin_lock(&dentry->d_lock);
8c6efb58
SW
48 if (dentry->d_fsdata) {
49 /* lost a race */
50 kmem_cache_free(ceph_dentry_cachep, di);
2817b000 51 goto out_unlock;
8c6efb58 52 }
48d0cbd1 53
8842b3be 54 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP)
48d0cbd1
SW
55 d_set_d_op(dentry, &ceph_dentry_ops);
56 else if (ceph_snap(dentry->d_parent->d_inode) == CEPH_SNAPDIR)
57 d_set_d_op(dentry, &ceph_snapdir_dentry_ops);
58 else
59 d_set_d_op(dentry, &ceph_snap_dentry_ops);
60
2817b000
SW
61 di->dentry = dentry;
62 di->lease_session = NULL;
2817b000 63 dentry->d_time = jiffies;
48d0cbd1
SW
64 /* avoid reordering d_fsdata setup so that the check above is safe */
65 smp_mb();
66 dentry->d_fsdata = di;
2817b000
SW
67 ceph_dentry_lru_add(dentry);
68out_unlock:
69 spin_unlock(&dentry->d_lock);
70 return 0;
71}
72
5f21c96d
SW
73struct inode *ceph_get_dentry_parent_inode(struct dentry *dentry)
74{
75 struct inode *inode = NULL;
76
77 if (!dentry)
78 return NULL;
79
80 spin_lock(&dentry->d_lock);
8842b3be 81 if (!IS_ROOT(dentry)) {
5f21c96d
SW
82 inode = dentry->d_parent->d_inode;
83 ihold(inode);
84 }
85 spin_unlock(&dentry->d_lock);
86 return inode;
87}
2817b000
SW
88
89
90/*
91 * for readdir, we encode the directory frag and offset within that
92 * frag into f_pos.
93 */
94static unsigned fpos_frag(loff_t p)
95{
96 return p >> 32;
97}
98static unsigned fpos_off(loff_t p)
99{
100 return p & 0xffffffff;
101}
102
4d5f5df6
YZ
103static int fpos_cmp(loff_t l, loff_t r)
104{
105 int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r));
106 if (v)
107 return v;
108 return (int)(fpos_off(l) - fpos_off(r));
109}
110
2817b000
SW
111/*
112 * When possible, we try to satisfy a readdir by peeking at the
113 * dcache. We make this work by carefully ordering dentries on
946e51f2 114 * d_child when we initially get results back from the MDS, and
2817b000
SW
115 * falling back to a "normal" sync readdir if any dentries in the dir
116 * are dropped.
117 *
2f276c51 118 * Complete dir indicates that we have all dentries in the dir. It is
2817b000
SW
119 * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
120 * the MDS if/when the directory is modified).
121 */
a30be7cb
YZ
122static int __dcache_readdir(struct file *file, struct dir_context *ctx,
123 u32 shared_gen)
2817b000 124{
77acfa29 125 struct ceph_file_info *fi = file->private_data;
b583043e 126 struct dentry *parent = file->f_path.dentry;
2817b000
SW
127 struct inode *dir = parent->d_inode;
128 struct list_head *p;
129 struct dentry *dentry, *last;
130 struct ceph_dentry_info *di;
131 int err = 0;
132
133 /* claim ref on last dentry we returned */
134 last = fi->dentry;
135 fi->dentry = NULL;
136
a30be7cb
YZ
137 dout("__dcache_readdir %p v%u at %llu (last %p)\n",
138 dir, shared_gen, ctx->pos, last);
2817b000 139
2fd6b7f5 140 spin_lock(&parent->d_lock);
2817b000
SW
141
142 /* start at beginning? */
77acfa29 143 if (ctx->pos == 2 || last == NULL ||
6da5246d 144 fpos_cmp(ctx->pos, ceph_dentry(last)->offset) < 0) {
2817b000
SW
145 if (list_empty(&parent->d_subdirs))
146 goto out_unlock;
147 p = parent->d_subdirs.prev;
148 dout(" initial p %p/%p\n", p->prev, p->next);
149 } else {
946e51f2 150 p = last->d_child.prev;
2817b000
SW
151 }
152
153more:
946e51f2 154 dentry = list_entry(p, struct dentry, d_child);
2817b000
SW
155 di = ceph_dentry(dentry);
156 while (1) {
1cd3935b
SW
157 dout(" p %p/%p %s d_subdirs %p/%p\n", p->prev, p->next,
158 d_unhashed(dentry) ? "!hashed" : "hashed",
2817b000
SW
159 parent->d_subdirs.prev, parent->d_subdirs.next);
160 if (p == &parent->d_subdirs) {
9cfa1098 161 fi->flags |= CEPH_F_ATEND;
2817b000
SW
162 goto out_unlock;
163 }
2fd6b7f5 164 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
a30be7cb
YZ
165 if (di->lease_shared_gen == shared_gen &&
166 !d_unhashed(dentry) && dentry->d_inode &&
09b8a7d2 167 ceph_snap(dentry->d_inode) != CEPH_SNAPDIR &&
1d1de916 168 ceph_ino(dentry->d_inode) != CEPH_INO_CEPH &&
4d5f5df6 169 fpos_cmp(ctx->pos, di->offset) <= 0)
2817b000 170 break;
a455589f
AV
171 dout(" skipping %p %pd at %llu (%llu)%s%s\n", dentry,
172 dentry, di->offset,
77acfa29 173 ctx->pos, d_unhashed(dentry) ? " unhashed" : "",
2817b000 174 !dentry->d_inode ? " null" : "");
da502956 175 spin_unlock(&dentry->d_lock);
2817b000 176 p = p->prev;
946e51f2 177 dentry = list_entry(p, struct dentry, d_child);
2817b000
SW
178 di = ceph_dentry(dentry);
179 }
180
da502956 181 dget_dlock(dentry);
b7ab39f6 182 spin_unlock(&dentry->d_lock);
2fd6b7f5 183 spin_unlock(&parent->d_lock);
2817b000 184
0081bd83 185 /* make sure a dentry wasn't dropped while we didn't have parent lock */
70db4f36 186 if (!ceph_dir_is_complete_ordered(dir)) {
0081bd83
YZ
187 dout(" lost dir complete on %p; falling back to mds\n", dir);
188 dput(dentry);
189 err = -EAGAIN;
190 goto out;
191 }
192
a455589f
AV
193 dout(" %llu (%llu) dentry %p %pd %p\n", di->offset, ctx->pos,
194 dentry, dentry, dentry->d_inode);
77acfa29
AV
195 if (!dir_emit(ctx, dentry->d_name.name,
196 dentry->d_name.len,
ad1fee96 197 ceph_translate_ino(dentry->d_sb, dentry->d_inode->i_ino),
77acfa29
AV
198 dentry->d_inode->i_mode >> 12)) {
199 if (last) {
2817b000
SW
200 /* remember our position */
201 fi->dentry = last;
f0494206 202 fi->next_offset = fpos_off(di->offset);
2817b000 203 }
77acfa29
AV
204 dput(dentry);
205 return 0;
2817b000 206 }
f5b06628 207
0081bd83
YZ
208 ctx->pos = di->offset + 1;
209
77acfa29
AV
210 if (last)
211 dput(last);
212 last = dentry;
2817b000 213
2fd6b7f5 214 spin_lock(&parent->d_lock);
efa4c120
SW
215 p = p->prev; /* advance to next dentry */
216 goto more;
2817b000
SW
217
218out_unlock:
2fd6b7f5 219 spin_unlock(&parent->d_lock);
efa4c120
SW
220out:
221 if (last)
2817b000 222 dput(last);
2817b000
SW
223 return err;
224}
225
226/*
227 * make note of the last dentry we read, so we can
228 * continue at the same lexicographical point,
229 * regardless of what dir changes take place on the
230 * server.
231 */
232static int note_last_dentry(struct ceph_file_info *fi, const char *name,
233 int len)
234{
235 kfree(fi->last_name);
236 fi->last_name = kmalloc(len+1, GFP_NOFS);
237 if (!fi->last_name)
238 return -ENOMEM;
239 memcpy(fi->last_name, name, len);
240 fi->last_name[len] = 0;
241 dout("note_last_dentry '%s'\n", fi->last_name);
242 return 0;
243}
244
77acfa29 245static int ceph_readdir(struct file *file, struct dir_context *ctx)
2817b000 246{
77acfa29
AV
247 struct ceph_file_info *fi = file->private_data;
248 struct inode *inode = file_inode(file);
2817b000 249 struct ceph_inode_info *ci = ceph_inode(inode);
3d14c5d2
YS
250 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
251 struct ceph_mds_client *mdsc = fsc->mdsc;
77acfa29
AV
252 unsigned frag = fpos_frag(ctx->pos);
253 int off = fpos_off(ctx->pos);
2817b000
SW
254 int err;
255 u32 ftype;
256 struct ceph_mds_reply_info_parsed *rinfo;
2817b000 257
77acfa29 258 dout("readdir %p file %p frag %u off %u\n", inode, file, frag, off);
9cfa1098 259 if (fi->flags & CEPH_F_ATEND)
2817b000
SW
260 return 0;
261
262 /* always start with . and .. */
77acfa29 263 if (ctx->pos == 0) {
2817b000 264 dout("readdir off 0 -> '.'\n");
77acfa29 265 if (!dir_emit(ctx, ".", 1,
ad1fee96 266 ceph_translate_ino(inode->i_sb, inode->i_ino),
77acfa29 267 inode->i_mode >> 12))
2817b000 268 return 0;
77acfa29 269 ctx->pos = 1;
2817b000
SW
270 off = 1;
271 }
77acfa29 272 if (ctx->pos == 1) {
b583043e 273 ino_t ino = parent_ino(file->f_path.dentry);
2817b000 274 dout("readdir off 1 -> '..'\n");
77acfa29 275 if (!dir_emit(ctx, "..", 2,
ad1fee96 276 ceph_translate_ino(inode->i_sb, ino),
77acfa29 277 inode->i_mode >> 12))
2817b000 278 return 0;
77acfa29 279 ctx->pos = 2;
2817b000
SW
280 off = 2;
281 }
282
283 /* can we use the dcache? */
be655596 284 spin_lock(&ci->i_ceph_lock);
77acfa29 285 if ((ctx->pos == 2 || fi->dentry) &&
3d14c5d2 286 !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
a0dff78d 287 ceph_snap(inode) != CEPH_SNAPDIR &&
70db4f36 288 __ceph_dir_is_complete_ordered(ci) &&
2817b000 289 __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) {
a30be7cb 290 u32 shared_gen = ci->i_shared_gen;
be655596 291 spin_unlock(&ci->i_ceph_lock);
a30be7cb 292 err = __dcache_readdir(file, ctx, shared_gen);
efa4c120 293 if (err != -EAGAIN)
2817b000 294 return err;
0081bd83
YZ
295 frag = fpos_frag(ctx->pos);
296 off = fpos_off(ctx->pos);
efa4c120 297 } else {
be655596 298 spin_unlock(&ci->i_ceph_lock);
2817b000 299 }
2817b000
SW
300 if (fi->dentry) {
301 err = note_last_dentry(fi, fi->dentry->d_name.name,
302 fi->dentry->d_name.len);
303 if (err)
304 return err;
305 dput(fi->dentry);
306 fi->dentry = NULL;
307 }
308
309 /* proceed with a normal readdir */
310
70db4f36
YZ
311 if (ctx->pos == 2) {
312 /* note dir version at start of readdir so we can tell
313 * if any dentries get dropped */
314 fi->dir_release_count = atomic_read(&ci->i_release_count);
315 fi->dir_ordered_count = ci->i_ordered_count;
316 }
317
2817b000
SW
318more:
319 /* do we have the correct frag content buffered? */
320 if (fi->frag != frag || fi->last_readdir == NULL) {
321 struct ceph_mds_request *req;
322 int op = ceph_snap(inode) == CEPH_SNAPDIR ?
323 CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;
324
325 /* discard old result, if any */
393f6620 326 if (fi->last_readdir) {
2817b000 327 ceph_mdsc_put_request(fi->last_readdir);
393f6620
SW
328 fi->last_readdir = NULL;
329 }
2817b000 330
2817b000
SW
331 dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
332 ceph_vinop(inode), frag, fi->last_name);
333 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
334 if (IS_ERR(req))
335 return PTR_ERR(req);
54008399
YZ
336 err = ceph_alloc_readdir_reply_buffer(req, inode);
337 if (err) {
338 ceph_mdsc_put_request(req);
339 return err;
340 }
70b666c3
SW
341 req->r_inode = inode;
342 ihold(inode);
b583043e 343 req->r_dentry = dget(file->f_path.dentry);
2817b000
SW
344 /* hints to request -> mds selection code */
345 req->r_direct_mode = USE_AUTH_MDS;
346 req->r_direct_hash = ceph_frag_value(frag);
347 req->r_direct_is_hash = true;
348 req->r_path2 = kstrdup(fi->last_name, GFP_NOFS);
349 req->r_readdir_offset = fi->next_offset;
350 req->r_args.readdir.frag = cpu_to_le32(frag);
2817b000
SW
351 err = ceph_mdsc_do_request(mdsc, NULL, req);
352 if (err < 0) {
353 ceph_mdsc_put_request(req);
354 return err;
355 }
356 dout("readdir got and parsed readdir result=%d"
357 " on frag %x, end=%d, complete=%d\n", err, frag,
358 (int)req->r_reply_info.dir_end,
359 (int)req->r_reply_info.dir_complete);
360
361 if (!req->r_did_prepopulate) {
362 dout("readdir !did_prepopulate");
2f276c51
YZ
363 /* preclude from marking dir complete */
364 fi->dir_release_count--;
2817b000
SW
365 }
366
367 /* note next offset and last dentry name */
81c6aea5
YZ
368 rinfo = &req->r_reply_info;
369 if (le32_to_cpu(rinfo->dir_dir->frag) != frag) {
370 frag = le32_to_cpu(rinfo->dir_dir->frag);
371 if (ceph_frag_is_leftmost(frag))
372 fi->next_offset = 2;
373 else
374 fi->next_offset = 0;
375 off = fi->next_offset;
376 }
f0494206 377 fi->frag = frag;
2817b000
SW
378 fi->offset = fi->next_offset;
379 fi->last_readdir = req;
380
381 if (req->r_reply_info.dir_end) {
382 kfree(fi->last_name);
383 fi->last_name = NULL;
7b88dadc
SW
384 if (ceph_frag_is_rightmost(frag))
385 fi->next_offset = 2;
386 else
387 fi->next_offset = 0;
2817b000 388 } else {
2817b000
SW
389 err = note_last_dentry(fi,
390 rinfo->dir_dname[rinfo->dir_nr-1],
391 rinfo->dir_dname_len[rinfo->dir_nr-1]);
392 if (err)
393 return err;
394 fi->next_offset += rinfo->dir_nr;
395 }
396 }
397
398 rinfo = &fi->last_readdir->r_reply_info;
399 dout("readdir frag %x num %d off %d chunkoff %d\n", frag,
400 rinfo->dir_nr, off, fi->offset);
77acfa29
AV
401
402 ctx->pos = ceph_make_fpos(frag, off);
da39822c 403 while (off >= fi->offset && off - fi->offset < rinfo->dir_nr) {
2817b000
SW
404 struct ceph_mds_reply_inode *in =
405 rinfo->dir_in[off - fi->offset].in;
3105c19c
SW
406 struct ceph_vino vino;
407 ino_t ino;
408
2817b000 409 dout("readdir off %d (%d/%d) -> %lld '%.*s' %p\n",
77acfa29 410 off, off - fi->offset, rinfo->dir_nr, ctx->pos,
2817b000
SW
411 rinfo->dir_dname_len[off - fi->offset],
412 rinfo->dir_dname[off - fi->offset], in);
413 BUG_ON(!in);
414 ftype = le32_to_cpu(in->mode) >> 12;
3105c19c
SW
415 vino.ino = le64_to_cpu(in->ino);
416 vino.snap = le64_to_cpu(in->snapid);
417 ino = ceph_vino_to_ino(vino);
77acfa29 418 if (!dir_emit(ctx,
2817b000
SW
419 rinfo->dir_dname[off - fi->offset],
420 rinfo->dir_dname_len[off - fi->offset],
77acfa29 421 ceph_translate_ino(inode->i_sb, ino), ftype)) {
2817b000
SW
422 dout("filldir stopping us...\n");
423 return 0;
424 }
425 off++;
77acfa29 426 ctx->pos++;
2817b000
SW
427 }
428
429 if (fi->last_name) {
430 ceph_mdsc_put_request(fi->last_readdir);
431 fi->last_readdir = NULL;
432 goto more;
433 }
434
435 /* more frags? */
436 if (!ceph_frag_is_rightmost(frag)) {
437 frag = ceph_frag_next(frag);
438 off = 0;
77acfa29 439 ctx->pos = ceph_make_fpos(frag, off);
2817b000
SW
440 dout("readdir next frag is %x\n", frag);
441 goto more;
442 }
9cfa1098 443 fi->flags |= CEPH_F_ATEND;
2817b000
SW
444
445 /*
446 * if dir_release_count still matches the dir, no dentries
447 * were released during the whole readdir, and we should have
448 * the complete dir contents in our cache.
449 */
be655596 450 spin_lock(&ci->i_ceph_lock);
2f276c51 451 if (atomic_read(&ci->i_release_count) == fi->dir_release_count) {
70db4f36
YZ
452 if (ci->i_ordered_count == fi->dir_ordered_count)
453 dout(" marking %p complete and ordered\n", inode);
454 else
455 dout(" marking %p complete\n", inode);
456 __ceph_dir_set_complete(ci, fi->dir_release_count,
457 fi->dir_ordered_count);
2817b000 458 }
be655596 459 spin_unlock(&ci->i_ceph_lock);
2817b000 460
77acfa29 461 dout("readdir %p file %p done.\n", inode, file);
2817b000
SW
462 return 0;
463}
464
dcd3cc05 465static void reset_readdir(struct ceph_file_info *fi, unsigned frag)
2817b000
SW
466{
467 if (fi->last_readdir) {
468 ceph_mdsc_put_request(fi->last_readdir);
469 fi->last_readdir = NULL;
470 }
471 kfree(fi->last_name);
a1629c3b 472 fi->last_name = NULL;
dcd3cc05
YZ
473 if (ceph_frag_is_leftmost(frag))
474 fi->next_offset = 2; /* compensate for . and .. */
475 else
476 fi->next_offset = 0;
2817b000
SW
477 if (fi->dentry) {
478 dput(fi->dentry);
479 fi->dentry = NULL;
480 }
9cfa1098 481 fi->flags &= ~CEPH_F_ATEND;
2817b000
SW
482}
483
965c8e59 484static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
2817b000
SW
485{
486 struct ceph_file_info *fi = file->private_data;
487 struct inode *inode = file->f_mapping->host;
f0494206 488 loff_t old_offset = ceph_make_fpos(fi->frag, fi->next_offset);
2817b000
SW
489 loff_t retval;
490
491 mutex_lock(&inode->i_mutex);
06222e49 492 retval = -EINVAL;
965c8e59 493 switch (whence) {
2817b000
SW
494 case SEEK_END:
495 offset += inode->i_size + 2; /* FIXME */
496 break;
497 case SEEK_CUR:
498 offset += file->f_pos;
06222e49
JB
499 case SEEK_SET:
500 break;
501 default:
502 goto out;
2817b000 503 }
06222e49 504
f0494206 505 if (offset >= 0) {
2817b000
SW
506 if (offset != file->f_pos) {
507 file->f_pos = offset;
508 file->f_version = 0;
9cfa1098 509 fi->flags &= ~CEPH_F_ATEND;
2817b000
SW
510 }
511 retval = offset;
512
513 /*
514 * discard buffered readdir content on seekdir(0), or
515 * seek to new frag, or seek prior to current chunk.
516 */
517 if (offset == 0 ||
f0494206 518 fpos_frag(offset) != fi->frag ||
2817b000
SW
519 fpos_off(offset) < fi->offset) {
520 dout("dir_llseek dropping %p content\n", file);
dcd3cc05 521 reset_readdir(fi, fpos_frag(offset));
2817b000
SW
522 }
523
524 /* bump dir_release_count if we did a forward seek */
f0494206 525 if (fpos_cmp(offset, old_offset) > 0)
2817b000
SW
526 fi->dir_release_count--;
527 }
06222e49 528out:
2817b000
SW
529 mutex_unlock(&inode->i_mutex);
530 return retval;
531}
532
533/*
468640e3 534 * Handle lookups for the hidden .snap directory.
2817b000 535 */
468640e3
SW
536int ceph_handle_snapdir(struct ceph_mds_request *req,
537 struct dentry *dentry, int err)
2817b000 538{
3d14c5d2 539 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
d79698da 540 struct inode *parent = dentry->d_parent->d_inode; /* we hold i_mutex */
2817b000
SW
541
542 /* .snap dir? */
543 if (err == -ENOENT &&
455cec0a 544 ceph_snap(parent) == CEPH_NOSNAP &&
6b805185 545 strcmp(dentry->d_name.name,
3d14c5d2 546 fsc->mount_options->snapdir_name) == 0) {
2817b000 547 struct inode *inode = ceph_get_snapdir(parent);
a455589f
AV
548 dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n",
549 dentry, dentry, inode);
9358c6d4 550 BUG_ON(!d_unhashed(dentry));
2817b000
SW
551 d_add(dentry, inode);
552 err = 0;
553 }
468640e3
SW
554 return err;
555}
2817b000 556
468640e3
SW
557/*
558 * Figure out final result of a lookup/open request.
559 *
560 * Mainly, make sure we return the final req->r_dentry (if it already
561 * existed) in place of the original VFS-provided dentry when they
562 * differ.
563 *
564 * Gracefully handle the case where the MDS replies with -ENOENT and
565 * no trace (which it may do, at its discretion, e.g., if it doesn't
566 * care to issue a lease on the negative dentry).
567 */
568struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
569 struct dentry *dentry, int err)
570{
2817b000
SW
571 if (err == -ENOENT) {
572 /* no trace? */
573 err = 0;
574 if (!req->r_reply_info.head->is_dentry) {
575 dout("ENOENT and no trace, dentry %p inode %p\n",
576 dentry, dentry->d_inode);
577 if (dentry->d_inode) {
578 d_drop(dentry);
579 err = -ENOENT;
580 } else {
581 d_add(dentry, NULL);
582 }
583 }
584 }
585 if (err)
586 dentry = ERR_PTR(err);
587 else if (dentry != req->r_dentry)
588 dentry = dget(req->r_dentry); /* we got spliced */
589 else
590 dentry = NULL;
591 return dentry;
592}
593
1d1de916
SW
594static int is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
595{
596 return ceph_ino(inode) == CEPH_INO_ROOT &&
597 strncmp(dentry->d_name.name, ".ceph", 5) == 0;
598}
599
2817b000
SW
600/*
601 * Look up a single dir entry. If there is a lookup intent, inform
602 * the MDS so that it gets our 'caps wanted' value in a single op.
603 */
604static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
00cd8dd3 605 unsigned int flags)
2817b000 606{
3d14c5d2
YS
607 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
608 struct ceph_mds_client *mdsc = fsc->mdsc;
2817b000
SW
609 struct ceph_mds_request *req;
610 int op;
611 int err;
612
a455589f
AV
613 dout("lookup %p dentry %p '%pd'\n",
614 dir, dentry, dentry);
2817b000
SW
615
616 if (dentry->d_name.len > NAME_MAX)
617 return ERR_PTR(-ENAMETOOLONG);
618
619 err = ceph_init_dentry(dentry);
620 if (err < 0)
621 return ERR_PTR(err);
622
2817b000
SW
623 /* can we conclude ENOENT locally? */
624 if (dentry->d_inode == NULL) {
625 struct ceph_inode_info *ci = ceph_inode(dir);
626 struct ceph_dentry_info *di = ceph_dentry(dentry);
627
be655596 628 spin_lock(&ci->i_ceph_lock);
2817b000
SW
629 dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags);
630 if (strncmp(dentry->d_name.name,
3d14c5d2 631 fsc->mount_options->snapdir_name,
2817b000 632 dentry->d_name.len) &&
1d1de916 633 !is_root_ceph_dentry(dir, dentry) &&
2f276c51 634 __ceph_dir_is_complete(ci) &&
2817b000 635 (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) {
be655596 636 spin_unlock(&ci->i_ceph_lock);
2817b000
SW
637 dout(" dir %p complete, -ENOENT\n", dir);
638 d_add(dentry, NULL);
639 di->lease_shared_gen = ci->i_shared_gen;
640 return NULL;
641 }
be655596 642 spin_unlock(&ci->i_ceph_lock);
2817b000
SW
643 }
644
645 op = ceph_snap(dir) == CEPH_SNAPDIR ?
646 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
647 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
648 if (IS_ERR(req))
7e34bc52 649 return ERR_CAST(req);
2817b000
SW
650 req->r_dentry = dget(dentry);
651 req->r_num_caps = 2;
652 /* we only need inode linkage */
653 req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
654 req->r_locked_dir = dir;
655 err = ceph_mdsc_do_request(mdsc, NULL, req);
468640e3 656 err = ceph_handle_snapdir(req, dentry, err);
2817b000
SW
657 dentry = ceph_finish_lookup(req, dentry, err);
658 ceph_mdsc_put_request(req); /* will dput(dentry) */
659 dout("lookup result=%p\n", dentry);
660 return dentry;
661}
662
663/*
664 * If we do a create but get no trace back from the MDS, follow up with
665 * a lookup (the VFS expects us to link up the provided dentry).
666 */
667int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
668{
00cd8dd3 669 struct dentry *result = ceph_lookup(dir, dentry, 0);
2817b000
SW
670
671 if (result && !IS_ERR(result)) {
672 /*
673 * We created the item, then did a lookup, and found
674 * it was already linked to another inode we already
675 * had in our cache (and thus got spliced). Link our
676 * dentry to that inode, but don't hash it, just in
677 * case the VFS wants to dereference it.
678 */
679 BUG_ON(!result->d_inode);
680 d_instantiate(dentry, result->d_inode);
681 return 0;
682 }
683 return PTR_ERR(result);
684}
685
686static int ceph_mknod(struct inode *dir, struct dentry *dentry,
1a67aafb 687 umode_t mode, dev_t rdev)
2817b000 688{
3d14c5d2
YS
689 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
690 struct ceph_mds_client *mdsc = fsc->mdsc;
2817b000 691 struct ceph_mds_request *req;
b1ee94aa 692 struct ceph_acls_info acls = {};
2817b000
SW
693 int err;
694
695 if (ceph_snap(dir) != CEPH_NOSNAP)
696 return -EROFS;
697
b1ee94aa
YZ
698 err = ceph_pre_init_acls(dir, &mode, &acls);
699 if (err < 0)
700 return err;
701
1a67aafb 702 dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
2817b000
SW
703 dir, dentry, mode, rdev);
704 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
705 if (IS_ERR(req)) {
b1ee94aa
YZ
706 err = PTR_ERR(req);
707 goto out;
2817b000
SW
708 }
709 req->r_dentry = dget(dentry);
710 req->r_num_caps = 2;
711 req->r_locked_dir = dir;
712 req->r_args.mknod.mode = cpu_to_le32(mode);
713 req->r_args.mknod.rdev = cpu_to_le32(rdev);
714 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
715 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
b1ee94aa
YZ
716 if (acls.pagelist) {
717 req->r_pagelist = acls.pagelist;
718 acls.pagelist = NULL;
719 }
2817b000
SW
720 err = ceph_mdsc_do_request(mdsc, dir, req);
721 if (!err && !req->r_reply_info.head->is_dentry)
722 err = ceph_handle_notrace_create(dir, dentry);
723 ceph_mdsc_put_request(req);
b1ee94aa 724out:
7221fe4c 725 if (!err)
b1ee94aa 726 ceph_init_inode_acls(dentry->d_inode, &acls);
b20a95a0 727 else
2817b000 728 d_drop(dentry);
b1ee94aa 729 ceph_release_acls_info(&acls);
2817b000
SW
730 return err;
731}
732
4acdaf27 733static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode,
ebfc3b49 734 bool excl)
2817b000 735{
2d83bde9 736 return ceph_mknod(dir, dentry, mode, 0);
2817b000
SW
737}
738
739static int ceph_symlink(struct inode *dir, struct dentry *dentry,
740 const char *dest)
741{
3d14c5d2
YS
742 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
743 struct ceph_mds_client *mdsc = fsc->mdsc;
2817b000
SW
744 struct ceph_mds_request *req;
745 int err;
746
747 if (ceph_snap(dir) != CEPH_NOSNAP)
748 return -EROFS;
749
750 dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest);
751 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
752 if (IS_ERR(req)) {
b1ee94aa
YZ
753 err = PTR_ERR(req);
754 goto out;
2817b000
SW
755 }
756 req->r_dentry = dget(dentry);
757 req->r_num_caps = 2;
758 req->r_path2 = kstrdup(dest, GFP_NOFS);
759 req->r_locked_dir = dir;
760 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
761 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
762 err = ceph_mdsc_do_request(mdsc, dir, req);
763 if (!err && !req->r_reply_info.head->is_dentry)
764 err = ceph_handle_notrace_create(dir, dentry);
765 ceph_mdsc_put_request(req);
b1ee94aa
YZ
766out:
767 if (err)
2817b000
SW
768 d_drop(dentry);
769 return err;
770}
771
18bb1db3 772static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2817b000 773{
3d14c5d2
YS
774 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
775 struct ceph_mds_client *mdsc = fsc->mdsc;
2817b000 776 struct ceph_mds_request *req;
b1ee94aa 777 struct ceph_acls_info acls = {};
2817b000
SW
778 int err = -EROFS;
779 int op;
780
781 if (ceph_snap(dir) == CEPH_SNAPDIR) {
782 /* mkdir .snap/foo is a MKSNAP */
783 op = CEPH_MDS_OP_MKSNAP;
a455589f
AV
784 dout("mksnap dir %p snap '%pd' dn %p\n", dir,
785 dentry, dentry);
2817b000 786 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
18bb1db3 787 dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode);
2817b000
SW
788 op = CEPH_MDS_OP_MKDIR;
789 } else {
790 goto out;
791 }
b1ee94aa
YZ
792
793 mode |= S_IFDIR;
794 err = ceph_pre_init_acls(dir, &mode, &acls);
795 if (err < 0)
796 goto out;
797
2817b000
SW
798 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
799 if (IS_ERR(req)) {
800 err = PTR_ERR(req);
801 goto out;
802 }
803
804 req->r_dentry = dget(dentry);
805 req->r_num_caps = 2;
806 req->r_locked_dir = dir;
807 req->r_args.mkdir.mode = cpu_to_le32(mode);
808 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
809 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
b1ee94aa
YZ
810 if (acls.pagelist) {
811 req->r_pagelist = acls.pagelist;
812 acls.pagelist = NULL;
813 }
2817b000 814 err = ceph_mdsc_do_request(mdsc, dir, req);
275dd19e
YZ
815 if (!err &&
816 !req->r_reply_info.head->is_target &&
817 !req->r_reply_info.head->is_dentry)
2817b000
SW
818 err = ceph_handle_notrace_create(dir, dentry);
819 ceph_mdsc_put_request(req);
820out:
b20a95a0 821 if (!err)
b1ee94aa 822 ceph_init_inode_acls(dentry->d_inode, &acls);
b20a95a0 823 else
2817b000 824 d_drop(dentry);
b1ee94aa 825 ceph_release_acls_info(&acls);
2817b000
SW
826 return err;
827}
828
829static int ceph_link(struct dentry *old_dentry, struct inode *dir,
830 struct dentry *dentry)
831{
3d14c5d2
YS
832 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
833 struct ceph_mds_client *mdsc = fsc->mdsc;
2817b000
SW
834 struct ceph_mds_request *req;
835 int err;
836
837 if (ceph_snap(dir) != CEPH_NOSNAP)
838 return -EROFS;
839
840 dout("link in dir %p old_dentry %p dentry %p\n", dir,
841 old_dentry, dentry);
842 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS);
843 if (IS_ERR(req)) {
844 d_drop(dentry);
845 return PTR_ERR(req);
846 }
847 req->r_dentry = dget(dentry);
848 req->r_num_caps = 2;
4b58c9b1 849 req->r_old_dentry = dget(old_dentry);
2817b000
SW
850 req->r_locked_dir = dir;
851 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
852 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
ad88f23f
YZ
853 /* release LINK_SHARED on source inode (mds will lock it) */
854 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
2817b000 855 err = ceph_mdsc_do_request(mdsc, dir, req);
70b666c3 856 if (err) {
2817b000 857 d_drop(dentry);
70b666c3
SW
858 } else if (!req->r_reply_info.head->is_dentry) {
859 ihold(old_dentry->d_inode);
860 d_instantiate(dentry, old_dentry->d_inode);
861 }
2817b000
SW
862 ceph_mdsc_put_request(req);
863 return err;
864}
865
866/*
867 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
868 * looks like the link count will hit 0, drop any other caps (other
869 * than PIN) we don't specifically want (due to the file still being
870 * open).
871 */
872static int drop_caps_for_unlink(struct inode *inode)
873{
874 struct ceph_inode_info *ci = ceph_inode(inode);
875 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
876
be655596 877 spin_lock(&ci->i_ceph_lock);
2817b000
SW
878 if (inode->i_nlink == 1) {
879 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
880 ci->i_ceph_flags |= CEPH_I_NODELAY;
881 }
be655596 882 spin_unlock(&ci->i_ceph_lock);
2817b000
SW
883 return drop;
884}
885
886/*
887 * rmdir and unlink are differ only by the metadata op code
888 */
889static int ceph_unlink(struct inode *dir, struct dentry *dentry)
890{
3d14c5d2
YS
891 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
892 struct ceph_mds_client *mdsc = fsc->mdsc;
2817b000
SW
893 struct inode *inode = dentry->d_inode;
894 struct ceph_mds_request *req;
895 int err = -EROFS;
896 int op;
897
898 if (ceph_snap(dir) == CEPH_SNAPDIR) {
899 /* rmdir .snap/foo is RMSNAP */
a455589f 900 dout("rmsnap dir %p '%pd' dn %p\n", dir, dentry, dentry);
2817b000
SW
901 op = CEPH_MDS_OP_RMSNAP;
902 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
903 dout("unlink/rmdir dir %p dn %p inode %p\n",
904 dir, dentry, inode);
dba19c60 905 op = S_ISDIR(dentry->d_inode->i_mode) ?
2817b000
SW
906 CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK;
907 } else
908 goto out;
909 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
910 if (IS_ERR(req)) {
911 err = PTR_ERR(req);
912 goto out;
913 }
914 req->r_dentry = dget(dentry);
915 req->r_num_caps = 2;
916 req->r_locked_dir = dir;
917 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
918 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
919 req->r_inode_drop = drop_caps_for_unlink(inode);
920 err = ceph_mdsc_do_request(mdsc, dir, req);
921 if (!err && !req->r_reply_info.head->is_dentry)
922 d_delete(dentry);
923 ceph_mdsc_put_request(req);
924out:
925 return err;
926}
927
928static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry,
929 struct inode *new_dir, struct dentry *new_dentry)
930{
3d14c5d2
YS
931 struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb);
932 struct ceph_mds_client *mdsc = fsc->mdsc;
2817b000
SW
933 struct ceph_mds_request *req;
934 int err;
935
936 if (ceph_snap(old_dir) != ceph_snap(new_dir))
937 return -EXDEV;
938 if (ceph_snap(old_dir) != CEPH_NOSNAP ||
939 ceph_snap(new_dir) != CEPH_NOSNAP)
940 return -EROFS;
941 dout("rename dir %p dentry %p to dir %p dentry %p\n",
942 old_dir, old_dentry, new_dir, new_dentry);
943 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RENAME, USE_AUTH_MDS);
944 if (IS_ERR(req))
945 return PTR_ERR(req);
180061a5 946 ihold(old_dir);
2817b000
SW
947 req->r_dentry = dget(new_dentry);
948 req->r_num_caps = 2;
949 req->r_old_dentry = dget(old_dentry);
180061a5 950 req->r_old_dentry_dir = old_dir;
2817b000
SW
951 req->r_locked_dir = new_dir;
952 req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED;
953 req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
954 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
955 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
956 /* release LINK_RDCACHE on source inode (mds will lock it) */
957 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
958 if (new_dentry->d_inode)
959 req->r_inode_drop = drop_caps_for_unlink(new_dentry->d_inode);
960 err = ceph_mdsc_do_request(mdsc, old_dir, req);
961 if (!err && !req->r_reply_info.head->is_dentry) {
962 /*
963 * Normally d_move() is done by fill_trace (called by
964 * do_request, above). If there is no trace, we need
965 * to do it here.
966 */
ea1409f9 967
2817b000 968 d_move(old_dentry, new_dentry);
ea1409f9
SW
969
970 /* ensure target dentry is invalidated, despite
971 rehashing bug in vfs_rename_dir */
81a6cf2d 972 ceph_invalidate_dentry_lease(new_dentry);
0a8a70f9
YZ
973
974 /* d_move screws up sibling dentries' offsets */
975 ceph_dir_clear_complete(old_dir);
976 ceph_dir_clear_complete(new_dir);
977
2817b000
SW
978 }
979 ceph_mdsc_put_request(req);
980 return err;
981}
982
81a6cf2d
SW
983/*
984 * Ensure a dentry lease will no longer revalidate.
985 */
986void ceph_invalidate_dentry_lease(struct dentry *dentry)
987{
988 spin_lock(&dentry->d_lock);
989 dentry->d_time = jiffies;
990 ceph_dentry(dentry)->lease_shared_gen = 0;
991 spin_unlock(&dentry->d_lock);
992}
2817b000
SW
993
994/*
995 * Check if dentry lease is valid. If not, delete the lease. Try to
996 * renew if the least is more than half up.
997 */
998static int dentry_lease_is_valid(struct dentry *dentry)
999{
1000 struct ceph_dentry_info *di;
1001 struct ceph_mds_session *s;
1002 int valid = 0;
1003 u32 gen;
1004 unsigned long ttl;
1005 struct ceph_mds_session *session = NULL;
1006 struct inode *dir = NULL;
1007 u32 seq = 0;
1008
1009 spin_lock(&dentry->d_lock);
1010 di = ceph_dentry(dentry);
3d8eb7a9 1011 if (di->lease_session) {
2817b000 1012 s = di->lease_session;
d8fb02ab 1013 spin_lock(&s->s_gen_ttl_lock);
2817b000
SW
1014 gen = s->s_cap_gen;
1015 ttl = s->s_cap_ttl;
d8fb02ab 1016 spin_unlock(&s->s_gen_ttl_lock);
2817b000
SW
1017
1018 if (di->lease_gen == gen &&
1019 time_before(jiffies, dentry->d_time) &&
1020 time_before(jiffies, ttl)) {
1021 valid = 1;
1022 if (di->lease_renew_after &&
1023 time_after(jiffies, di->lease_renew_after)) {
1024 /* we should renew */
1025 dir = dentry->d_parent->d_inode;
1026 session = ceph_get_mds_session(s);
1027 seq = di->lease_seq;
1028 di->lease_renew_after = 0;
1029 di->lease_renew_from = jiffies;
1030 }
2817b000
SW
1031 }
1032 }
1033 spin_unlock(&dentry->d_lock);
1034
1035 if (session) {
1036 ceph_mdsc_lease_send_msg(session, dir, dentry,
1037 CEPH_MDS_LEASE_RENEW, seq);
1038 ceph_put_mds_session(session);
1039 }
1040 dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid);
1041 return valid;
1042}
1043
1044/*
1045 * Check if directory-wide content lease/cap is valid.
1046 */
1047static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry)
1048{
1049 struct ceph_inode_info *ci = ceph_inode(dir);
1050 struct ceph_dentry_info *di = ceph_dentry(dentry);
1051 int valid = 0;
1052
be655596 1053 spin_lock(&ci->i_ceph_lock);
2817b000
SW
1054 if (ci->i_shared_gen == di->lease_shared_gen)
1055 valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
be655596 1056 spin_unlock(&ci->i_ceph_lock);
2817b000
SW
1057 dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n",
1058 dir, (unsigned)ci->i_shared_gen, dentry,
1059 (unsigned)di->lease_shared_gen, valid);
1060 return valid;
1061}
1062
1063/*
1064 * Check if cached dentry can be trusted.
1065 */
0b728e19 1066static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags)
2817b000 1067{
bf1c6aca 1068 int valid = 0;
34286d66
NP
1069 struct inode *dir;
1070
0b728e19 1071 if (flags & LOOKUP_RCU)
34286d66
NP
1072 return -ECHILD;
1073
a455589f
AV
1074 dout("d_revalidate %p '%pd' inode %p offset %lld\n", dentry,
1075 dentry, dentry->d_inode, ceph_dentry(dentry)->offset);
2817b000 1076
bf1c6aca
SW
1077 dir = ceph_get_dentry_parent_inode(dentry);
1078
2817b000
SW
1079 /* always trust cached snapped dentries, snapdir dentry */
1080 if (ceph_snap(dir) != CEPH_NOSNAP) {
a455589f
AV
1081 dout("d_revalidate %p '%pd' inode %p is SNAPPED\n", dentry,
1082 dentry, dentry->d_inode);
bf1c6aca
SW
1083 valid = 1;
1084 } else if (dentry->d_inode &&
1085 ceph_snap(dentry->d_inode) == CEPH_SNAPDIR) {
1086 valid = 1;
1087 } else if (dentry_lease_is_valid(dentry) ||
1088 dir_lease_is_valid(dir, dentry)) {
9215aeea
YZ
1089 if (dentry->d_inode)
1090 valid = ceph_is_any_caps(dentry->d_inode);
1091 else
1092 valid = 1;
2817b000 1093 }
2817b000 1094
bf1c6aca 1095 dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid");
9215aeea 1096 if (valid) {
bf1c6aca 1097 ceph_dentry_lru_touch(dentry);
9215aeea
YZ
1098 } else {
1099 ceph_dir_clear_complete(dir);
9215aeea 1100 }
bf1c6aca
SW
1101 iput(dir);
1102 return valid;
2817b000
SW
1103}
1104
1105/*
147851d2 1106 * Release our ceph_dentry_info.
2817b000 1107 */
147851d2 1108static void ceph_d_release(struct dentry *dentry)
2817b000
SW
1109{
1110 struct ceph_dentry_info *di = ceph_dentry(dentry);
2817b000 1111
147851d2 1112 dout("d_release %p\n", dentry);
3d8eb7a9
SW
1113 ceph_dentry_lru_del(dentry);
1114 if (di->lease_session)
1115 ceph_put_mds_session(di->lease_session);
1116 kmem_cache_free(ceph_dentry_cachep, di);
1117 dentry->d_fsdata = NULL;
2817b000
SW
1118}
1119
1120static int ceph_snapdir_d_revalidate(struct dentry *dentry,
0b728e19 1121 unsigned int flags)
2817b000
SW
1122{
1123 /*
1124 * Eventually, we'll want to revalidate snapped metadata
1125 * too... probably...
1126 */
1127 return 1;
1128}
1129
b58dc410
SW
1130/*
1131 * When the VFS prunes a dentry from the cache, we need to clear the
1132 * complete flag on the parent directory.
1133 *
1134 * Called under dentry->d_lock.
1135 */
1136static void ceph_d_prune(struct dentry *dentry)
1137{
774ac21d 1138 dout("ceph_d_prune %p\n", dentry);
b58dc410
SW
1139
1140 /* do we have a valid parent? */
8842b3be 1141 if (IS_ROOT(dentry))
b58dc410
SW
1142 return;
1143
2f276c51 1144 /* if we are not hashed, we don't affect dir's completeness */
b58dc410
SW
1145 if (d_unhashed(dentry))
1146 return;
2817b000 1147
b58dc410
SW
1148 /*
1149 * we hold d_lock, so d_parent is stable, and d_fsdata is never
1150 * cleared until d_release
1151 */
2f276c51 1152 ceph_dir_clear_complete(dentry->d_parent->d_inode);
b58dc410 1153}
2817b000
SW
1154
1155/*
1156 * read() on a dir. This weird interface hack only works if mounted
1157 * with '-o dirstat'.
1158 */
1159static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
1160 loff_t *ppos)
1161{
1162 struct ceph_file_info *cf = file->private_data;
496ad9aa 1163 struct inode *inode = file_inode(file);
2817b000
SW
1164 struct ceph_inode_info *ci = ceph_inode(inode);
1165 int left;
ae598083 1166 const int bufsize = 1024;
2817b000 1167
3d14c5d2 1168 if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT))
2817b000
SW
1169 return -EISDIR;
1170
1171 if (!cf->dir_info) {
ae598083 1172 cf->dir_info = kmalloc(bufsize, GFP_NOFS);
2817b000
SW
1173 if (!cf->dir_info)
1174 return -ENOMEM;
1175 cf->dir_info_len =
ae598083 1176 snprintf(cf->dir_info, bufsize,
2817b000
SW
1177 "entries: %20lld\n"
1178 " files: %20lld\n"
1179 " subdirs: %20lld\n"
1180 "rentries: %20lld\n"
1181 " rfiles: %20lld\n"
1182 " rsubdirs: %20lld\n"
1183 "rbytes: %20lld\n"
1184 "rctime: %10ld.%09ld\n",
1185 ci->i_files + ci->i_subdirs,
1186 ci->i_files,
1187 ci->i_subdirs,
1188 ci->i_rfiles + ci->i_rsubdirs,
1189 ci->i_rfiles,
1190 ci->i_rsubdirs,
1191 ci->i_rbytes,
1192 (long)ci->i_rctime.tv_sec,
1193 (long)ci->i_rctime.tv_nsec);
1194 }
1195
1196 if (*ppos >= cf->dir_info_len)
1197 return 0;
1198 size = min_t(unsigned, size, cf->dir_info_len-*ppos);
1199 left = copy_to_user(buf, cf->dir_info + *ppos, size);
1200 if (left == size)
1201 return -EFAULT;
1202 *ppos += (size - left);
1203 return size - left;
1204}
1205
1206/*
1207 * an fsync() on a dir will wait for any uncommitted directory
1208 * operations to commit.
1209 */
02c24a82
JB
1210static int ceph_dir_fsync(struct file *file, loff_t start, loff_t end,
1211 int datasync)
2817b000 1212{
496ad9aa 1213 struct inode *inode = file_inode(file);
2817b000
SW
1214 struct ceph_inode_info *ci = ceph_inode(inode);
1215 struct list_head *head = &ci->i_unsafe_dirops;
1216 struct ceph_mds_request *req;
1217 u64 last_tid;
1218 int ret = 0;
1219
1220 dout("dir_fsync %p\n", inode);
02c24a82
JB
1221 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
1222 if (ret)
1223 return ret;
1224 mutex_lock(&inode->i_mutex);
1225
2817b000
SW
1226 spin_lock(&ci->i_unsafe_lock);
1227 if (list_empty(head))
1228 goto out;
1229
1230 req = list_entry(head->prev,
1231 struct ceph_mds_request, r_unsafe_dir_item);
1232 last_tid = req->r_tid;
1233
1234 do {
1235 ceph_mdsc_get_request(req);
1236 spin_unlock(&ci->i_unsafe_lock);
2ff179e6 1237
2817b000
SW
1238 dout("dir_fsync %p wait on tid %llu (until %llu)\n",
1239 inode, req->r_tid, last_tid);
1240 if (req->r_timeout) {
1241 ret = wait_for_completion_timeout(
1242 &req->r_safe_completion, req->r_timeout);
1243 if (ret > 0)
1244 ret = 0;
1245 else if (ret == 0)
1246 ret = -EIO; /* timed out */
1247 } else {
1248 wait_for_completion(&req->r_safe_completion);
1249 }
2817b000
SW
1250 ceph_mdsc_put_request(req);
1251
2ff179e6 1252 spin_lock(&ci->i_unsafe_lock);
2817b000
SW
1253 if (ret || list_empty(head))
1254 break;
1255 req = list_entry(head->next,
1256 struct ceph_mds_request, r_unsafe_dir_item);
1257 } while (req->r_tid < last_tid);
1258out:
1259 spin_unlock(&ci->i_unsafe_lock);
02c24a82
JB
1260 mutex_unlock(&inode->i_mutex);
1261
2817b000
SW
1262 return ret;
1263}
1264
1265/*
1266 * We maintain a private dentry LRU.
1267 *
1268 * FIXME: this needs to be changed to a per-mds lru to be useful.
1269 */
1270void ceph_dentry_lru_add(struct dentry *dn)
1271{
1272 struct ceph_dentry_info *di = ceph_dentry(dn);
1273 struct ceph_mds_client *mdsc;
2817b000 1274
a455589f 1275 dout("dentry_lru_add %p %p '%pd'\n", di, dn, dn);
3d8eb7a9
SW
1276 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1277 spin_lock(&mdsc->dentry_lru_lock);
1278 list_add_tail(&di->lru, &mdsc->dentry_lru);
1279 mdsc->num_dentry++;
1280 spin_unlock(&mdsc->dentry_lru_lock);
2817b000
SW
1281}
1282
1283void ceph_dentry_lru_touch(struct dentry *dn)
1284{
1285 struct ceph_dentry_info *di = ceph_dentry(dn);
1286 struct ceph_mds_client *mdsc;
2817b000 1287
a455589f
AV
1288 dout("dentry_lru_touch %p %p '%pd' (offset %lld)\n", di, dn, dn,
1289 di->offset);
3d8eb7a9
SW
1290 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1291 spin_lock(&mdsc->dentry_lru_lock);
1292 list_move_tail(&di->lru, &mdsc->dentry_lru);
1293 spin_unlock(&mdsc->dentry_lru_lock);
2817b000
SW
1294}
1295
1296void ceph_dentry_lru_del(struct dentry *dn)
1297{
1298 struct ceph_dentry_info *di = ceph_dentry(dn);
1299 struct ceph_mds_client *mdsc;
1300
a455589f 1301 dout("dentry_lru_del %p %p '%pd'\n", di, dn, dn);
3d8eb7a9
SW
1302 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1303 spin_lock(&mdsc->dentry_lru_lock);
1304 list_del_init(&di->lru);
1305 mdsc->num_dentry--;
1306 spin_unlock(&mdsc->dentry_lru_lock);
2817b000
SW
1307}
1308
6c0f3af7
SW
1309/*
1310 * Return name hash for a given dentry. This is dependent on
1311 * the parent directory's hash function.
1312 */
e5f86dc3 1313unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
6c0f3af7 1314{
6c0f3af7
SW
1315 struct ceph_inode_info *dci = ceph_inode(dir);
1316
1317 switch (dci->i_dir_layout.dl_dir_hash) {
1318 case 0: /* for backward compat */
1319 case CEPH_STR_HASH_LINUX:
1320 return dn->d_name.hash;
1321
1322 default:
1323 return ceph_str_hash(dci->i_dir_layout.dl_dir_hash,
1324 dn->d_name.name, dn->d_name.len);
1325 }
1326}
1327
2817b000
SW
1328const struct file_operations ceph_dir_fops = {
1329 .read = ceph_read_dir,
77acfa29 1330 .iterate = ceph_readdir,
2817b000
SW
1331 .llseek = ceph_dir_llseek,
1332 .open = ceph_open,
1333 .release = ceph_release,
1334 .unlocked_ioctl = ceph_ioctl,
1335 .fsync = ceph_dir_fsync,
1336};
1337
1338const struct inode_operations ceph_dir_iops = {
1339 .lookup = ceph_lookup,
1340 .permission = ceph_permission,
1341 .getattr = ceph_getattr,
1342 .setattr = ceph_setattr,
1343 .setxattr = ceph_setxattr,
1344 .getxattr = ceph_getxattr,
1345 .listxattr = ceph_listxattr,
1346 .removexattr = ceph_removexattr,
7221fe4c 1347 .get_acl = ceph_get_acl,
72466d0b 1348 .set_acl = ceph_set_acl,
2817b000
SW
1349 .mknod = ceph_mknod,
1350 .symlink = ceph_symlink,
1351 .mkdir = ceph_mkdir,
1352 .link = ceph_link,
1353 .unlink = ceph_unlink,
1354 .rmdir = ceph_unlink,
1355 .rename = ceph_rename,
1356 .create = ceph_create,
2d83bde9 1357 .atomic_open = ceph_atomic_open,
2817b000
SW
1358};
1359
52dfb8ac 1360const struct dentry_operations ceph_dentry_ops = {
2817b000 1361 .d_revalidate = ceph_d_revalidate,
147851d2 1362 .d_release = ceph_d_release,
b58dc410 1363 .d_prune = ceph_d_prune,
2817b000
SW
1364};
1365
52dfb8ac 1366const struct dentry_operations ceph_snapdir_dentry_ops = {
2817b000 1367 .d_revalidate = ceph_snapdir_d_revalidate,
147851d2 1368 .d_release = ceph_d_release,
2817b000
SW
1369};
1370
52dfb8ac 1371const struct dentry_operations ceph_snap_dentry_ops = {
147851d2 1372 .d_release = ceph_d_release,
b58dc410 1373 .d_prune = ceph_d_prune,
2817b000 1374};