dir.c 35 KB

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