dir.c 36 KB

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