dir.c 35 KB

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