dir.c 34 KB

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