PageRenderTime 53ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/ceph/dir.c

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