dir.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  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 <linux/xattr.h>
  8. #include "super.h"
  9. #include "mds_client.h"
  10. /*
  11. * Directory operations: readdir, lookup, create, link, unlink,
  12. * rename, etc.
  13. */
  14. /*
  15. * Ceph MDS operations are specified in terms of a base ino and
  16. * relative path. Thus, the client can specify an operation on a
  17. * specific inode (e.g., a getattr due to fstat(2)), or as a path
  18. * relative to, say, the root directory.
  19. *
  20. * Normally, we limit ourselves to strict inode ops (no path component)
  21. * or dentry operations (a single path component relative to an ino). The
  22. * exception to this is open_root_dentry(), which will open the mount
  23. * point by name.
  24. */
  25. const struct dentry_operations ceph_dentry_ops;
  26. /*
  27. * Initialize ceph dentry state.
  28. */
  29. int ceph_init_dentry(struct dentry *dentry)
  30. {
  31. struct ceph_dentry_info *di;
  32. if (dentry->d_fsdata)
  33. return 0;
  34. di = kmem_cache_zalloc(ceph_dentry_cachep, GFP_KERNEL);
  35. if (!di)
  36. return -ENOMEM; /* oh well */
  37. spin_lock(&dentry->d_lock);
  38. if (dentry->d_fsdata) {
  39. /* lost a race */
  40. kmem_cache_free(ceph_dentry_cachep, di);
  41. goto out_unlock;
  42. }
  43. if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_NOSNAP)
  44. d_set_d_op(dentry, &ceph_dentry_ops);
  45. else if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_SNAPDIR)
  46. d_set_d_op(dentry, &ceph_snapdir_dentry_ops);
  47. else
  48. d_set_d_op(dentry, &ceph_snap_dentry_ops);
  49. di->dentry = dentry;
  50. di->lease_session = NULL;
  51. di->time = jiffies;
  52. /* avoid reordering d_fsdata setup so that the check above is safe */
  53. smp_mb();
  54. dentry->d_fsdata = di;
  55. ceph_dentry_lru_add(dentry);
  56. out_unlock:
  57. spin_unlock(&dentry->d_lock);
  58. return 0;
  59. }
  60. /*
  61. * for f_pos for readdir:
  62. * - hash order:
  63. * (0xff << 52) | ((24 bits hash) << 28) |
  64. * (the nth entry has hash collision);
  65. * - frag+name order;
  66. * ((frag value) << 28) | (the nth entry in frag);
  67. */
  68. #define OFFSET_BITS 28
  69. #define OFFSET_MASK ((1 << OFFSET_BITS) - 1)
  70. #define HASH_ORDER (0xffull << (OFFSET_BITS + 24))
  71. loff_t ceph_make_fpos(unsigned high, unsigned off, bool hash_order)
  72. {
  73. loff_t fpos = ((loff_t)high << 28) | (loff_t)off;
  74. if (hash_order)
  75. fpos |= HASH_ORDER;
  76. return fpos;
  77. }
  78. static bool is_hash_order(loff_t p)
  79. {
  80. return (p & HASH_ORDER) == HASH_ORDER;
  81. }
  82. static unsigned fpos_frag(loff_t p)
  83. {
  84. return p >> OFFSET_BITS;
  85. }
  86. static unsigned fpos_hash(loff_t p)
  87. {
  88. return ceph_frag_value(fpos_frag(p));
  89. }
  90. static unsigned fpos_off(loff_t p)
  91. {
  92. return p & OFFSET_MASK;
  93. }
  94. static int fpos_cmp(loff_t l, loff_t r)
  95. {
  96. int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r));
  97. if (v)
  98. return v;
  99. return (int)(fpos_off(l) - fpos_off(r));
  100. }
  101. /*
  102. * make note of the last dentry we read, so we can
  103. * continue at the same lexicographical point,
  104. * regardless of what dir changes take place on the
  105. * server.
  106. */
  107. static int note_last_dentry(struct ceph_file_info *fi, const char *name,
  108. int len, unsigned next_offset)
  109. {
  110. char *buf = kmalloc(len+1, GFP_KERNEL);
  111. if (!buf)
  112. return -ENOMEM;
  113. kfree(fi->last_name);
  114. fi->last_name = buf;
  115. memcpy(fi->last_name, name, len);
  116. fi->last_name[len] = 0;
  117. fi->next_offset = next_offset;
  118. dout("note_last_dentry '%s'\n", fi->last_name);
  119. return 0;
  120. }
  121. static struct dentry *
  122. __dcache_find_get_entry(struct dentry *parent, u64 idx,
  123. struct ceph_readdir_cache_control *cache_ctl)
  124. {
  125. struct inode *dir = d_inode(parent);
  126. struct dentry *dentry;
  127. unsigned idx_mask = (PAGE_SIZE / sizeof(struct dentry *)) - 1;
  128. loff_t ptr_pos = idx * sizeof(struct dentry *);
  129. pgoff_t ptr_pgoff = ptr_pos >> PAGE_SHIFT;
  130. if (ptr_pos >= i_size_read(dir))
  131. return NULL;
  132. if (!cache_ctl->page || ptr_pgoff != page_index(cache_ctl->page)) {
  133. ceph_readdir_cache_release(cache_ctl);
  134. cache_ctl->page = find_lock_page(&dir->i_data, ptr_pgoff);
  135. if (!cache_ctl->page) {
  136. dout(" page %lu not found\n", ptr_pgoff);
  137. return ERR_PTR(-EAGAIN);
  138. }
  139. /* reading/filling the cache are serialized by
  140. i_mutex, no need to use page lock */
  141. unlock_page(cache_ctl->page);
  142. cache_ctl->dentries = kmap(cache_ctl->page);
  143. }
  144. cache_ctl->index = idx & idx_mask;
  145. rcu_read_lock();
  146. spin_lock(&parent->d_lock);
  147. /* check i_size again here, because empty directory can be
  148. * marked as complete while not holding the i_mutex. */
  149. if (ceph_dir_is_complete_ordered(dir) && ptr_pos < i_size_read(dir))
  150. dentry = cache_ctl->dentries[cache_ctl->index];
  151. else
  152. dentry = NULL;
  153. spin_unlock(&parent->d_lock);
  154. if (dentry && !lockref_get_not_dead(&dentry->d_lockref))
  155. dentry = NULL;
  156. rcu_read_unlock();
  157. return dentry ? : ERR_PTR(-EAGAIN);
  158. }
  159. /*
  160. * When possible, we try to satisfy a readdir by peeking at the
  161. * dcache. We make this work by carefully ordering dentries on
  162. * d_child when we initially get results back from the MDS, and
  163. * falling back to a "normal" sync readdir if any dentries in the dir
  164. * are dropped.
  165. *
  166. * Complete dir indicates that we have all dentries in the dir. It is
  167. * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
  168. * the MDS if/when the directory is modified).
  169. */
  170. static int __dcache_readdir(struct file *file, struct dir_context *ctx,
  171. u32 shared_gen)
  172. {
  173. struct ceph_file_info *fi = file->private_data;
  174. struct dentry *parent = file->f_path.dentry;
  175. struct inode *dir = d_inode(parent);
  176. struct dentry *dentry, *last = NULL;
  177. struct ceph_dentry_info *di;
  178. struct ceph_readdir_cache_control cache_ctl = {};
  179. u64 idx = 0;
  180. int err = 0;
  181. dout("__dcache_readdir %p v%u at %llx\n", dir, shared_gen, ctx->pos);
  182. /* search start position */
  183. if (ctx->pos > 2) {
  184. u64 count = div_u64(i_size_read(dir), sizeof(struct dentry *));
  185. while (count > 0) {
  186. u64 step = count >> 1;
  187. dentry = __dcache_find_get_entry(parent, idx + step,
  188. &cache_ctl);
  189. if (!dentry) {
  190. /* use linar search */
  191. idx = 0;
  192. break;
  193. }
  194. if (IS_ERR(dentry)) {
  195. err = PTR_ERR(dentry);
  196. goto out;
  197. }
  198. di = ceph_dentry(dentry);
  199. spin_lock(&dentry->d_lock);
  200. if (fpos_cmp(di->offset, ctx->pos) < 0) {
  201. idx += step + 1;
  202. count -= step + 1;
  203. } else {
  204. count = step;
  205. }
  206. spin_unlock(&dentry->d_lock);
  207. dput(dentry);
  208. }
  209. dout("__dcache_readdir %p cache idx %llu\n", dir, idx);
  210. }
  211. for (;;) {
  212. bool emit_dentry = false;
  213. dentry = __dcache_find_get_entry(parent, idx++, &cache_ctl);
  214. if (!dentry) {
  215. fi->flags |= CEPH_F_ATEND;
  216. err = 0;
  217. break;
  218. }
  219. if (IS_ERR(dentry)) {
  220. err = PTR_ERR(dentry);
  221. goto out;
  222. }
  223. di = ceph_dentry(dentry);
  224. spin_lock(&dentry->d_lock);
  225. if (di->lease_shared_gen == shared_gen &&
  226. d_really_is_positive(dentry) &&
  227. fpos_cmp(ctx->pos, di->offset) <= 0) {
  228. emit_dentry = true;
  229. }
  230. spin_unlock(&dentry->d_lock);
  231. if (emit_dentry) {
  232. dout(" %llx dentry %p %pd %p\n", di->offset,
  233. dentry, dentry, d_inode(dentry));
  234. ctx->pos = di->offset;
  235. if (!dir_emit(ctx, dentry->d_name.name,
  236. dentry->d_name.len,
  237. ceph_translate_ino(dentry->d_sb,
  238. d_inode(dentry)->i_ino),
  239. d_inode(dentry)->i_mode >> 12)) {
  240. dput(dentry);
  241. err = 0;
  242. break;
  243. }
  244. ctx->pos++;
  245. if (last)
  246. dput(last);
  247. last = dentry;
  248. } else {
  249. dput(dentry);
  250. }
  251. }
  252. out:
  253. ceph_readdir_cache_release(&cache_ctl);
  254. if (last) {
  255. int ret;
  256. di = ceph_dentry(last);
  257. ret = note_last_dentry(fi, last->d_name.name, last->d_name.len,
  258. fpos_off(di->offset) + 1);
  259. if (ret < 0)
  260. err = ret;
  261. dput(last);
  262. /* last_name no longer match cache index */
  263. if (fi->readdir_cache_idx >= 0) {
  264. fi->readdir_cache_idx = -1;
  265. fi->dir_release_count = 0;
  266. }
  267. }
  268. return err;
  269. }
  270. static bool need_send_readdir(struct ceph_file_info *fi, loff_t pos)
  271. {
  272. if (!fi->last_readdir)
  273. return true;
  274. if (is_hash_order(pos))
  275. return !ceph_frag_contains_value(fi->frag, fpos_hash(pos));
  276. else
  277. return fi->frag != fpos_frag(pos);
  278. }
  279. static int ceph_readdir(struct file *file, struct dir_context *ctx)
  280. {
  281. struct ceph_file_info *fi = file->private_data;
  282. struct inode *inode = file_inode(file);
  283. struct ceph_inode_info *ci = ceph_inode(inode);
  284. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  285. struct ceph_mds_client *mdsc = fsc->mdsc;
  286. int i;
  287. int err;
  288. unsigned frag = -1;
  289. struct ceph_mds_reply_info_parsed *rinfo;
  290. dout("readdir %p file %p pos %llx\n", inode, file, ctx->pos);
  291. if (fi->flags & CEPH_F_ATEND)
  292. return 0;
  293. /* always start with . and .. */
  294. if (ctx->pos == 0) {
  295. dout("readdir off 0 -> '.'\n");
  296. if (!dir_emit(ctx, ".", 1,
  297. ceph_translate_ino(inode->i_sb, inode->i_ino),
  298. inode->i_mode >> 12))
  299. return 0;
  300. ctx->pos = 1;
  301. }
  302. if (ctx->pos == 1) {
  303. ino_t ino = parent_ino(file->f_path.dentry);
  304. dout("readdir off 1 -> '..'\n");
  305. if (!dir_emit(ctx, "..", 2,
  306. ceph_translate_ino(inode->i_sb, ino),
  307. inode->i_mode >> 12))
  308. return 0;
  309. ctx->pos = 2;
  310. }
  311. /* can we use the dcache? */
  312. spin_lock(&ci->i_ceph_lock);
  313. if (ceph_test_mount_opt(fsc, DCACHE) &&
  314. !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
  315. ceph_snap(inode) != CEPH_SNAPDIR &&
  316. __ceph_dir_is_complete_ordered(ci) &&
  317. __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) {
  318. u32 shared_gen = ci->i_shared_gen;
  319. spin_unlock(&ci->i_ceph_lock);
  320. err = __dcache_readdir(file, ctx, shared_gen);
  321. if (err != -EAGAIN)
  322. return err;
  323. } else {
  324. spin_unlock(&ci->i_ceph_lock);
  325. }
  326. /* proceed with a normal readdir */
  327. more:
  328. /* do we have the correct frag content buffered? */
  329. if (need_send_readdir(fi, ctx->pos)) {
  330. struct ceph_mds_request *req;
  331. int op = ceph_snap(inode) == CEPH_SNAPDIR ?
  332. CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;
  333. /* discard old result, if any */
  334. if (fi->last_readdir) {
  335. ceph_mdsc_put_request(fi->last_readdir);
  336. fi->last_readdir = NULL;
  337. }
  338. if (is_hash_order(ctx->pos)) {
  339. /* fragtree isn't always accurate. choose frag
  340. * based on previous reply when possible. */
  341. if (frag == (unsigned)-1)
  342. frag = ceph_choose_frag(ci, fpos_hash(ctx->pos),
  343. NULL, NULL);
  344. } else {
  345. frag = fpos_frag(ctx->pos);
  346. }
  347. dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
  348. ceph_vinop(inode), frag, fi->last_name);
  349. req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
  350. if (IS_ERR(req))
  351. return PTR_ERR(req);
  352. err = ceph_alloc_readdir_reply_buffer(req, inode);
  353. if (err) {
  354. ceph_mdsc_put_request(req);
  355. return err;
  356. }
  357. /* hints to request -> mds selection code */
  358. req->r_direct_mode = USE_AUTH_MDS;
  359. req->r_direct_hash = ceph_frag_value(frag);
  360. req->r_direct_is_hash = true;
  361. if (fi->last_name) {
  362. req->r_path2 = kstrdup(fi->last_name, GFP_KERNEL);
  363. if (!req->r_path2) {
  364. ceph_mdsc_put_request(req);
  365. return -ENOMEM;
  366. }
  367. }
  368. req->r_dir_release_cnt = fi->dir_release_count;
  369. req->r_dir_ordered_cnt = fi->dir_ordered_count;
  370. req->r_readdir_cache_idx = fi->readdir_cache_idx;
  371. req->r_readdir_offset = fi->next_offset;
  372. req->r_args.readdir.frag = cpu_to_le32(frag);
  373. req->r_args.readdir.flags =
  374. cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS);
  375. req->r_inode = inode;
  376. ihold(inode);
  377. req->r_dentry = dget(file->f_path.dentry);
  378. err = ceph_mdsc_do_request(mdsc, NULL, req);
  379. if (err < 0) {
  380. ceph_mdsc_put_request(req);
  381. return err;
  382. }
  383. dout("readdir got and parsed readdir result=%d on "
  384. "frag %x, end=%d, complete=%d, hash_order=%d\n",
  385. err, frag,
  386. (int)req->r_reply_info.dir_end,
  387. (int)req->r_reply_info.dir_complete,
  388. (int)req->r_reply_info.hash_order);
  389. rinfo = &req->r_reply_info;
  390. if (le32_to_cpu(rinfo->dir_dir->frag) != frag) {
  391. frag = le32_to_cpu(rinfo->dir_dir->frag);
  392. if (!rinfo->hash_order) {
  393. fi->next_offset = req->r_readdir_offset;
  394. /* adjust ctx->pos to beginning of frag */
  395. ctx->pos = ceph_make_fpos(frag,
  396. fi->next_offset,
  397. false);
  398. }
  399. }
  400. fi->frag = frag;
  401. fi->last_readdir = req;
  402. if (req->r_did_prepopulate) {
  403. fi->readdir_cache_idx = req->r_readdir_cache_idx;
  404. if (fi->readdir_cache_idx < 0) {
  405. /* preclude from marking dir ordered */
  406. fi->dir_ordered_count = 0;
  407. } else if (ceph_frag_is_leftmost(frag) &&
  408. fi->next_offset == 2) {
  409. /* note dir version at start of readdir so
  410. * we can tell if any dentries get dropped */
  411. fi->dir_release_count = req->r_dir_release_cnt;
  412. fi->dir_ordered_count = req->r_dir_ordered_cnt;
  413. }
  414. } else {
  415. dout("readdir !did_prepopulate");
  416. /* disable readdir cache */
  417. fi->readdir_cache_idx = -1;
  418. /* preclude from marking dir complete */
  419. fi->dir_release_count = 0;
  420. }
  421. /* note next offset and last dentry name */
  422. if (rinfo->dir_nr > 0) {
  423. struct ceph_mds_reply_dir_entry *rde =
  424. rinfo->dir_entries + (rinfo->dir_nr-1);
  425. unsigned next_offset = req->r_reply_info.dir_end ?
  426. 2 : (fpos_off(rde->offset) + 1);
  427. err = note_last_dentry(fi, rde->name, rde->name_len,
  428. next_offset);
  429. if (err)
  430. return err;
  431. } else if (req->r_reply_info.dir_end) {
  432. fi->next_offset = 2;
  433. /* keep last name */
  434. }
  435. }
  436. rinfo = &fi->last_readdir->r_reply_info;
  437. dout("readdir frag %x num %d pos %llx chunk first %llx\n",
  438. fi->frag, rinfo->dir_nr, ctx->pos,
  439. rinfo->dir_nr ? rinfo->dir_entries[0].offset : 0LL);
  440. i = 0;
  441. /* search start position */
  442. if (rinfo->dir_nr > 0) {
  443. int step, nr = rinfo->dir_nr;
  444. while (nr > 0) {
  445. step = nr >> 1;
  446. if (rinfo->dir_entries[i + step].offset < ctx->pos) {
  447. i += step + 1;
  448. nr -= step + 1;
  449. } else {
  450. nr = step;
  451. }
  452. }
  453. }
  454. for (; i < rinfo->dir_nr; i++) {
  455. struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
  456. struct ceph_vino vino;
  457. ino_t ino;
  458. u32 ftype;
  459. BUG_ON(rde->offset < ctx->pos);
  460. ctx->pos = rde->offset;
  461. dout("readdir (%d/%d) -> %llx '%.*s' %p\n",
  462. i, rinfo->dir_nr, ctx->pos,
  463. rde->name_len, rde->name, &rde->inode.in);
  464. BUG_ON(!rde->inode.in);
  465. ftype = le32_to_cpu(rde->inode.in->mode) >> 12;
  466. vino.ino = le64_to_cpu(rde->inode.in->ino);
  467. vino.snap = le64_to_cpu(rde->inode.in->snapid);
  468. ino = ceph_vino_to_ino(vino);
  469. if (!dir_emit(ctx, rde->name, rde->name_len,
  470. ceph_translate_ino(inode->i_sb, ino), ftype)) {
  471. dout("filldir stopping us...\n");
  472. return 0;
  473. }
  474. ctx->pos++;
  475. }
  476. ceph_mdsc_put_request(fi->last_readdir);
  477. fi->last_readdir = NULL;
  478. if (fi->next_offset > 2) {
  479. frag = fi->frag;
  480. goto more;
  481. }
  482. /* more frags? */
  483. if (!ceph_frag_is_rightmost(fi->frag)) {
  484. frag = ceph_frag_next(fi->frag);
  485. if (is_hash_order(ctx->pos)) {
  486. loff_t new_pos = ceph_make_fpos(ceph_frag_value(frag),
  487. fi->next_offset, true);
  488. if (new_pos > ctx->pos)
  489. ctx->pos = new_pos;
  490. /* keep last_name */
  491. } else {
  492. ctx->pos = ceph_make_fpos(frag, fi->next_offset, false);
  493. kfree(fi->last_name);
  494. fi->last_name = NULL;
  495. }
  496. dout("readdir next frag is %x\n", frag);
  497. goto more;
  498. }
  499. fi->flags |= CEPH_F_ATEND;
  500. /*
  501. * if dir_release_count still matches the dir, no dentries
  502. * were released during the whole readdir, and we should have
  503. * the complete dir contents in our cache.
  504. */
  505. if (atomic64_read(&ci->i_release_count) == fi->dir_release_count) {
  506. spin_lock(&ci->i_ceph_lock);
  507. if (fi->dir_ordered_count == atomic64_read(&ci->i_ordered_count)) {
  508. dout(" marking %p complete and ordered\n", inode);
  509. /* use i_size to track number of entries in
  510. * readdir cache */
  511. BUG_ON(fi->readdir_cache_idx < 0);
  512. i_size_write(inode, fi->readdir_cache_idx *
  513. sizeof(struct dentry*));
  514. } else {
  515. dout(" marking %p complete\n", inode);
  516. }
  517. __ceph_dir_set_complete(ci, fi->dir_release_count,
  518. fi->dir_ordered_count);
  519. spin_unlock(&ci->i_ceph_lock);
  520. }
  521. dout("readdir %p file %p done.\n", inode, file);
  522. return 0;
  523. }
  524. static void reset_readdir(struct ceph_file_info *fi)
  525. {
  526. if (fi->last_readdir) {
  527. ceph_mdsc_put_request(fi->last_readdir);
  528. fi->last_readdir = NULL;
  529. }
  530. kfree(fi->last_name);
  531. fi->last_name = NULL;
  532. fi->dir_release_count = 0;
  533. fi->readdir_cache_idx = -1;
  534. fi->next_offset = 2; /* compensate for . and .. */
  535. fi->flags &= ~CEPH_F_ATEND;
  536. }
  537. /*
  538. * discard buffered readdir content on seekdir(0), or seek to new frag,
  539. * or seek prior to current chunk
  540. */
  541. static bool need_reset_readdir(struct ceph_file_info *fi, loff_t new_pos)
  542. {
  543. struct ceph_mds_reply_info_parsed *rinfo;
  544. loff_t chunk_offset;
  545. if (new_pos == 0)
  546. return true;
  547. if (is_hash_order(new_pos)) {
  548. /* no need to reset last_name for a forward seek when
  549. * dentries are sotred in hash order */
  550. } else if (fi->frag != fpos_frag(new_pos)) {
  551. return true;
  552. }
  553. rinfo = fi->last_readdir ? &fi->last_readdir->r_reply_info : NULL;
  554. if (!rinfo || !rinfo->dir_nr)
  555. return true;
  556. chunk_offset = rinfo->dir_entries[0].offset;
  557. return new_pos < chunk_offset ||
  558. is_hash_order(new_pos) != is_hash_order(chunk_offset);
  559. }
  560. static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
  561. {
  562. struct ceph_file_info *fi = file->private_data;
  563. struct inode *inode = file->f_mapping->host;
  564. loff_t retval;
  565. inode_lock(inode);
  566. retval = -EINVAL;
  567. switch (whence) {
  568. case SEEK_CUR:
  569. offset += file->f_pos;
  570. case SEEK_SET:
  571. break;
  572. case SEEK_END:
  573. retval = -EOPNOTSUPP;
  574. default:
  575. goto out;
  576. }
  577. if (offset >= 0) {
  578. if (need_reset_readdir(fi, offset)) {
  579. dout("dir_llseek dropping %p content\n", file);
  580. reset_readdir(fi);
  581. } else if (is_hash_order(offset) && offset > file->f_pos) {
  582. /* for hash offset, we don't know if a forward seek
  583. * is within same frag */
  584. fi->dir_release_count = 0;
  585. fi->readdir_cache_idx = -1;
  586. }
  587. if (offset != file->f_pos) {
  588. file->f_pos = offset;
  589. file->f_version = 0;
  590. fi->flags &= ~CEPH_F_ATEND;
  591. }
  592. retval = offset;
  593. }
  594. out:
  595. inode_unlock(inode);
  596. return retval;
  597. }
  598. /*
  599. * Handle lookups for the hidden .snap directory.
  600. */
  601. int ceph_handle_snapdir(struct ceph_mds_request *req,
  602. struct dentry *dentry, int err)
  603. {
  604. struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
  605. struct inode *parent = d_inode(dentry->d_parent); /* we hold i_mutex */
  606. /* .snap dir? */
  607. if (err == -ENOENT &&
  608. ceph_snap(parent) == CEPH_NOSNAP &&
  609. strcmp(dentry->d_name.name,
  610. fsc->mount_options->snapdir_name) == 0) {
  611. struct inode *inode = ceph_get_snapdir(parent);
  612. dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n",
  613. dentry, dentry, inode);
  614. BUG_ON(!d_unhashed(dentry));
  615. d_add(dentry, inode);
  616. err = 0;
  617. }
  618. return err;
  619. }
  620. /*
  621. * Figure out final result of a lookup/open request.
  622. *
  623. * Mainly, make sure we return the final req->r_dentry (if it already
  624. * existed) in place of the original VFS-provided dentry when they
  625. * differ.
  626. *
  627. * Gracefully handle the case where the MDS replies with -ENOENT and
  628. * no trace (which it may do, at its discretion, e.g., if it doesn't
  629. * care to issue a lease on the negative dentry).
  630. */
  631. struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
  632. struct dentry *dentry, int err)
  633. {
  634. if (err == -ENOENT) {
  635. /* no trace? */
  636. err = 0;
  637. if (!req->r_reply_info.head->is_dentry) {
  638. dout("ENOENT and no trace, dentry %p inode %p\n",
  639. dentry, d_inode(dentry));
  640. if (d_really_is_positive(dentry)) {
  641. d_drop(dentry);
  642. err = -ENOENT;
  643. } else {
  644. d_add(dentry, NULL);
  645. }
  646. }
  647. }
  648. if (err)
  649. dentry = ERR_PTR(err);
  650. else if (dentry != req->r_dentry)
  651. dentry = dget(req->r_dentry); /* we got spliced */
  652. else
  653. dentry = NULL;
  654. return dentry;
  655. }
  656. static bool is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
  657. {
  658. return ceph_ino(inode) == CEPH_INO_ROOT &&
  659. strncmp(dentry->d_name.name, ".ceph", 5) == 0;
  660. }
  661. /*
  662. * Look up a single dir entry. If there is a lookup intent, inform
  663. * the MDS so that it gets our 'caps wanted' value in a single op.
  664. */
  665. static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
  666. unsigned int flags)
  667. {
  668. struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
  669. struct ceph_mds_client *mdsc = fsc->mdsc;
  670. struct ceph_mds_request *req;
  671. int op;
  672. int mask;
  673. int err;
  674. dout("lookup %p dentry %p '%pd'\n",
  675. dir, dentry, dentry);
  676. if (dentry->d_name.len > NAME_MAX)
  677. return ERR_PTR(-ENAMETOOLONG);
  678. err = ceph_init_dentry(dentry);
  679. if (err < 0)
  680. return ERR_PTR(err);
  681. /* can we conclude ENOENT locally? */
  682. if (d_really_is_negative(dentry)) {
  683. struct ceph_inode_info *ci = ceph_inode(dir);
  684. struct ceph_dentry_info *di = ceph_dentry(dentry);
  685. spin_lock(&ci->i_ceph_lock);
  686. dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags);
  687. if (strncmp(dentry->d_name.name,
  688. fsc->mount_options->snapdir_name,
  689. dentry->d_name.len) &&
  690. !is_root_ceph_dentry(dir, dentry) &&
  691. ceph_test_mount_opt(fsc, DCACHE) &&
  692. __ceph_dir_is_complete(ci) &&
  693. (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) {
  694. spin_unlock(&ci->i_ceph_lock);
  695. dout(" dir %p complete, -ENOENT\n", dir);
  696. d_add(dentry, NULL);
  697. di->lease_shared_gen = ci->i_shared_gen;
  698. return NULL;
  699. }
  700. spin_unlock(&ci->i_ceph_lock);
  701. }
  702. op = ceph_snap(dir) == CEPH_SNAPDIR ?
  703. CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
  704. req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
  705. if (IS_ERR(req))
  706. return ERR_CAST(req);
  707. req->r_dentry = dget(dentry);
  708. req->r_num_caps = 2;
  709. mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
  710. if (ceph_security_xattr_wanted(dir))
  711. mask |= CEPH_CAP_XATTR_SHARED;
  712. req->r_args.getattr.mask = cpu_to_le32(mask);
  713. req->r_locked_dir = dir;
  714. err = ceph_mdsc_do_request(mdsc, NULL, req);
  715. err = ceph_handle_snapdir(req, dentry, err);
  716. dentry = ceph_finish_lookup(req, dentry, err);
  717. ceph_mdsc_put_request(req); /* will dput(dentry) */
  718. dout("lookup result=%p\n", dentry);
  719. return dentry;
  720. }
  721. /*
  722. * If we do a create but get no trace back from the MDS, follow up with
  723. * a lookup (the VFS expects us to link up the provided dentry).
  724. */
  725. int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
  726. {
  727. struct dentry *result = ceph_lookup(dir, dentry, 0);
  728. if (result && !IS_ERR(result)) {
  729. /*
  730. * We created the item, then did a lookup, and found
  731. * it was already linked to another inode we already
  732. * had in our cache (and thus got spliced). To not
  733. * confuse VFS (especially when inode is a directory),
  734. * we don't link our dentry to that inode, return an
  735. * error instead.
  736. *
  737. * This event should be rare and it happens only when
  738. * we talk to old MDS. Recent MDS does not send traceless
  739. * reply for request that creates new inode.
  740. */
  741. d_drop(result);
  742. return -ESTALE;
  743. }
  744. return PTR_ERR(result);
  745. }
  746. static int ceph_mknod(struct inode *dir, struct dentry *dentry,
  747. umode_t mode, dev_t rdev)
  748. {
  749. struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
  750. struct ceph_mds_client *mdsc = fsc->mdsc;
  751. struct ceph_mds_request *req;
  752. struct ceph_acls_info acls = {};
  753. int err;
  754. if (ceph_snap(dir) != CEPH_NOSNAP)
  755. return -EROFS;
  756. err = ceph_pre_init_acls(dir, &mode, &acls);
  757. if (err < 0)
  758. return err;
  759. dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
  760. dir, dentry, mode, rdev);
  761. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
  762. if (IS_ERR(req)) {
  763. err = PTR_ERR(req);
  764. goto out;
  765. }
  766. req->r_dentry = dget(dentry);
  767. req->r_num_caps = 2;
  768. req->r_locked_dir = dir;
  769. req->r_args.mknod.mode = cpu_to_le32(mode);
  770. req->r_args.mknod.rdev = cpu_to_le32(rdev);
  771. req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
  772. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  773. if (acls.pagelist) {
  774. req->r_pagelist = acls.pagelist;
  775. acls.pagelist = NULL;
  776. }
  777. err = ceph_mdsc_do_request(mdsc, dir, req);
  778. if (!err && !req->r_reply_info.head->is_dentry)
  779. err = ceph_handle_notrace_create(dir, dentry);
  780. ceph_mdsc_put_request(req);
  781. out:
  782. if (!err)
  783. ceph_init_inode_acls(d_inode(dentry), &acls);
  784. else
  785. d_drop(dentry);
  786. ceph_release_acls_info(&acls);
  787. return err;
  788. }
  789. static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  790. bool excl)
  791. {
  792. return ceph_mknod(dir, dentry, mode, 0);
  793. }
  794. static int ceph_symlink(struct inode *dir, struct dentry *dentry,
  795. const char *dest)
  796. {
  797. struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
  798. struct ceph_mds_client *mdsc = fsc->mdsc;
  799. struct ceph_mds_request *req;
  800. int err;
  801. if (ceph_snap(dir) != CEPH_NOSNAP)
  802. return -EROFS;
  803. dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest);
  804. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
  805. if (IS_ERR(req)) {
  806. err = PTR_ERR(req);
  807. goto out;
  808. }
  809. req->r_path2 = kstrdup(dest, GFP_KERNEL);
  810. if (!req->r_path2) {
  811. err = -ENOMEM;
  812. ceph_mdsc_put_request(req);
  813. goto out;
  814. }
  815. req->r_locked_dir = dir;
  816. req->r_dentry = dget(dentry);
  817. req->r_num_caps = 2;
  818. req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
  819. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  820. err = ceph_mdsc_do_request(mdsc, dir, req);
  821. if (!err && !req->r_reply_info.head->is_dentry)
  822. err = ceph_handle_notrace_create(dir, dentry);
  823. ceph_mdsc_put_request(req);
  824. out:
  825. if (err)
  826. d_drop(dentry);
  827. return err;
  828. }
  829. static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  830. {
  831. struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
  832. struct ceph_mds_client *mdsc = fsc->mdsc;
  833. struct ceph_mds_request *req;
  834. struct ceph_acls_info acls = {};
  835. int err = -EROFS;
  836. int op;
  837. if (ceph_snap(dir) == CEPH_SNAPDIR) {
  838. /* mkdir .snap/foo is a MKSNAP */
  839. op = CEPH_MDS_OP_MKSNAP;
  840. dout("mksnap dir %p snap '%pd' dn %p\n", dir,
  841. dentry, dentry);
  842. } else if (ceph_snap(dir) == CEPH_NOSNAP) {
  843. dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode);
  844. op = CEPH_MDS_OP_MKDIR;
  845. } else {
  846. goto out;
  847. }
  848. mode |= S_IFDIR;
  849. err = ceph_pre_init_acls(dir, &mode, &acls);
  850. if (err < 0)
  851. goto out;
  852. req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
  853. if (IS_ERR(req)) {
  854. err = PTR_ERR(req);
  855. goto out;
  856. }
  857. req->r_dentry = dget(dentry);
  858. req->r_num_caps = 2;
  859. req->r_locked_dir = dir;
  860. req->r_args.mkdir.mode = cpu_to_le32(mode);
  861. req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
  862. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  863. if (acls.pagelist) {
  864. req->r_pagelist = acls.pagelist;
  865. acls.pagelist = NULL;
  866. }
  867. err = ceph_mdsc_do_request(mdsc, dir, req);
  868. if (!err &&
  869. !req->r_reply_info.head->is_target &&
  870. !req->r_reply_info.head->is_dentry)
  871. err = ceph_handle_notrace_create(dir, dentry);
  872. ceph_mdsc_put_request(req);
  873. out:
  874. if (!err)
  875. ceph_init_inode_acls(d_inode(dentry), &acls);
  876. else
  877. d_drop(dentry);
  878. ceph_release_acls_info(&acls);
  879. return err;
  880. }
  881. static int ceph_link(struct dentry *old_dentry, struct inode *dir,
  882. struct dentry *dentry)
  883. {
  884. struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
  885. struct ceph_mds_client *mdsc = fsc->mdsc;
  886. struct ceph_mds_request *req;
  887. int err;
  888. if (ceph_snap(dir) != CEPH_NOSNAP)
  889. return -EROFS;
  890. dout("link in dir %p old_dentry %p dentry %p\n", dir,
  891. old_dentry, dentry);
  892. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS);
  893. if (IS_ERR(req)) {
  894. d_drop(dentry);
  895. return PTR_ERR(req);
  896. }
  897. req->r_dentry = dget(dentry);
  898. req->r_num_caps = 2;
  899. req->r_old_dentry = dget(old_dentry);
  900. req->r_locked_dir = dir;
  901. req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
  902. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  903. /* release LINK_SHARED on source inode (mds will lock it) */
  904. req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
  905. err = ceph_mdsc_do_request(mdsc, dir, req);
  906. if (err) {
  907. d_drop(dentry);
  908. } else if (!req->r_reply_info.head->is_dentry) {
  909. ihold(d_inode(old_dentry));
  910. d_instantiate(dentry, d_inode(old_dentry));
  911. }
  912. ceph_mdsc_put_request(req);
  913. return err;
  914. }
  915. /*
  916. * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
  917. * looks like the link count will hit 0, drop any other caps (other
  918. * than PIN) we don't specifically want (due to the file still being
  919. * open).
  920. */
  921. static int drop_caps_for_unlink(struct inode *inode)
  922. {
  923. struct ceph_inode_info *ci = ceph_inode(inode);
  924. int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
  925. spin_lock(&ci->i_ceph_lock);
  926. if (inode->i_nlink == 1) {
  927. drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
  928. ci->i_ceph_flags |= CEPH_I_NODELAY;
  929. }
  930. spin_unlock(&ci->i_ceph_lock);
  931. return drop;
  932. }
  933. /*
  934. * rmdir and unlink are differ only by the metadata op code
  935. */
  936. static int ceph_unlink(struct inode *dir, struct dentry *dentry)
  937. {
  938. struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
  939. struct ceph_mds_client *mdsc = fsc->mdsc;
  940. struct inode *inode = d_inode(dentry);
  941. struct ceph_mds_request *req;
  942. int err = -EROFS;
  943. int op;
  944. if (ceph_snap(dir) == CEPH_SNAPDIR) {
  945. /* rmdir .snap/foo is RMSNAP */
  946. dout("rmsnap dir %p '%pd' dn %p\n", dir, dentry, dentry);
  947. op = CEPH_MDS_OP_RMSNAP;
  948. } else if (ceph_snap(dir) == CEPH_NOSNAP) {
  949. dout("unlink/rmdir dir %p dn %p inode %p\n",
  950. dir, dentry, inode);
  951. op = d_is_dir(dentry) ?
  952. CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK;
  953. } else
  954. goto out;
  955. req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
  956. if (IS_ERR(req)) {
  957. err = PTR_ERR(req);
  958. goto out;
  959. }
  960. req->r_dentry = dget(dentry);
  961. req->r_num_caps = 2;
  962. req->r_locked_dir = dir;
  963. req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
  964. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  965. req->r_inode_drop = drop_caps_for_unlink(inode);
  966. err = ceph_mdsc_do_request(mdsc, dir, req);
  967. if (!err && !req->r_reply_info.head->is_dentry)
  968. d_delete(dentry);
  969. ceph_mdsc_put_request(req);
  970. out:
  971. return err;
  972. }
  973. static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry,
  974. struct inode *new_dir, struct dentry *new_dentry,
  975. unsigned int flags)
  976. {
  977. struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb);
  978. struct ceph_mds_client *mdsc = fsc->mdsc;
  979. struct ceph_mds_request *req;
  980. int op = CEPH_MDS_OP_RENAME;
  981. int err;
  982. if (flags)
  983. return -EINVAL;
  984. if (ceph_snap(old_dir) != ceph_snap(new_dir))
  985. return -EXDEV;
  986. if (ceph_snap(old_dir) != CEPH_NOSNAP) {
  987. if (old_dir == new_dir && ceph_snap(old_dir) == CEPH_SNAPDIR)
  988. op = CEPH_MDS_OP_RENAMESNAP;
  989. else
  990. return -EROFS;
  991. }
  992. dout("rename dir %p dentry %p to dir %p dentry %p\n",
  993. old_dir, old_dentry, new_dir, new_dentry);
  994. req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
  995. if (IS_ERR(req))
  996. return PTR_ERR(req);
  997. ihold(old_dir);
  998. req->r_dentry = dget(new_dentry);
  999. req->r_num_caps = 2;
  1000. req->r_old_dentry = dget(old_dentry);
  1001. req->r_old_dentry_dir = old_dir;
  1002. req->r_locked_dir = new_dir;
  1003. req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED;
  1004. req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
  1005. req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
  1006. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  1007. /* release LINK_RDCACHE on source inode (mds will lock it) */
  1008. req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
  1009. if (d_really_is_positive(new_dentry))
  1010. req->r_inode_drop = drop_caps_for_unlink(d_inode(new_dentry));
  1011. err = ceph_mdsc_do_request(mdsc, old_dir, req);
  1012. if (!err && !req->r_reply_info.head->is_dentry) {
  1013. /*
  1014. * Normally d_move() is done by fill_trace (called by
  1015. * do_request, above). If there is no trace, we need
  1016. * to do it here.
  1017. */
  1018. /* d_move screws up sibling dentries' offsets */
  1019. ceph_dir_clear_complete(old_dir);
  1020. ceph_dir_clear_complete(new_dir);
  1021. d_move(old_dentry, new_dentry);
  1022. /* ensure target dentry is invalidated, despite
  1023. rehashing bug in vfs_rename_dir */
  1024. ceph_invalidate_dentry_lease(new_dentry);
  1025. }
  1026. ceph_mdsc_put_request(req);
  1027. return err;
  1028. }
  1029. /*
  1030. * Ensure a dentry lease will no longer revalidate.
  1031. */
  1032. void ceph_invalidate_dentry_lease(struct dentry *dentry)
  1033. {
  1034. spin_lock(&dentry->d_lock);
  1035. ceph_dentry(dentry)->time = jiffies;
  1036. ceph_dentry(dentry)->lease_shared_gen = 0;
  1037. spin_unlock(&dentry->d_lock);
  1038. }
  1039. /*
  1040. * Check if dentry lease is valid. If not, delete the lease. Try to
  1041. * renew if the least is more than half up.
  1042. */
  1043. static int dentry_lease_is_valid(struct dentry *dentry, unsigned int flags,
  1044. struct inode *dir)
  1045. {
  1046. struct ceph_dentry_info *di;
  1047. struct ceph_mds_session *s;
  1048. int valid = 0;
  1049. u32 gen;
  1050. unsigned long ttl;
  1051. struct ceph_mds_session *session = NULL;
  1052. u32 seq = 0;
  1053. spin_lock(&dentry->d_lock);
  1054. di = ceph_dentry(dentry);
  1055. if (di && di->lease_session) {
  1056. s = di->lease_session;
  1057. spin_lock(&s->s_gen_ttl_lock);
  1058. gen = s->s_cap_gen;
  1059. ttl = s->s_cap_ttl;
  1060. spin_unlock(&s->s_gen_ttl_lock);
  1061. if (di->lease_gen == gen &&
  1062. time_before(jiffies, di->time) &&
  1063. time_before(jiffies, ttl)) {
  1064. valid = 1;
  1065. if (di->lease_renew_after &&
  1066. time_after(jiffies, di->lease_renew_after)) {
  1067. /*
  1068. * We should renew. If we're in RCU walk mode
  1069. * though, we can't do that so just return
  1070. * -ECHILD.
  1071. */
  1072. if (flags & LOOKUP_RCU) {
  1073. valid = -ECHILD;
  1074. } else {
  1075. session = ceph_get_mds_session(s);
  1076. seq = di->lease_seq;
  1077. di->lease_renew_after = 0;
  1078. di->lease_renew_from = jiffies;
  1079. }
  1080. }
  1081. }
  1082. }
  1083. spin_unlock(&dentry->d_lock);
  1084. if (session) {
  1085. ceph_mdsc_lease_send_msg(session, dir, dentry,
  1086. CEPH_MDS_LEASE_RENEW, seq);
  1087. ceph_put_mds_session(session);
  1088. }
  1089. dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid);
  1090. return valid;
  1091. }
  1092. /*
  1093. * Check if directory-wide content lease/cap is valid.
  1094. */
  1095. static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry)
  1096. {
  1097. struct ceph_inode_info *ci = ceph_inode(dir);
  1098. struct ceph_dentry_info *di = ceph_dentry(dentry);
  1099. int valid = 0;
  1100. spin_lock(&ci->i_ceph_lock);
  1101. if (ci->i_shared_gen == di->lease_shared_gen)
  1102. valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
  1103. spin_unlock(&ci->i_ceph_lock);
  1104. dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n",
  1105. dir, (unsigned)ci->i_shared_gen, dentry,
  1106. (unsigned)di->lease_shared_gen, valid);
  1107. return valid;
  1108. }
  1109. /*
  1110. * Check if cached dentry can be trusted.
  1111. */
  1112. static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags)
  1113. {
  1114. int valid = 0;
  1115. struct dentry *parent;
  1116. struct inode *dir;
  1117. if (flags & LOOKUP_RCU) {
  1118. parent = ACCESS_ONCE(dentry->d_parent);
  1119. dir = d_inode_rcu(parent);
  1120. if (!dir)
  1121. return -ECHILD;
  1122. } else {
  1123. parent = dget_parent(dentry);
  1124. dir = d_inode(parent);
  1125. }
  1126. dout("d_revalidate %p '%pd' inode %p offset %lld\n", dentry,
  1127. dentry, d_inode(dentry), ceph_dentry(dentry)->offset);
  1128. /* always trust cached snapped dentries, snapdir dentry */
  1129. if (ceph_snap(dir) != CEPH_NOSNAP) {
  1130. dout("d_revalidate %p '%pd' inode %p is SNAPPED\n", dentry,
  1131. dentry, d_inode(dentry));
  1132. valid = 1;
  1133. } else if (d_really_is_positive(dentry) &&
  1134. ceph_snap(d_inode(dentry)) == CEPH_SNAPDIR) {
  1135. valid = 1;
  1136. } else {
  1137. valid = dentry_lease_is_valid(dentry, flags, dir);
  1138. if (valid == -ECHILD)
  1139. return valid;
  1140. if (valid || dir_lease_is_valid(dir, dentry)) {
  1141. if (d_really_is_positive(dentry))
  1142. valid = ceph_is_any_caps(d_inode(dentry));
  1143. else
  1144. valid = 1;
  1145. }
  1146. }
  1147. if (!valid) {
  1148. struct ceph_mds_client *mdsc =
  1149. ceph_sb_to_client(dir->i_sb)->mdsc;
  1150. struct ceph_mds_request *req;
  1151. int op, err;
  1152. u32 mask;
  1153. if (flags & LOOKUP_RCU)
  1154. return -ECHILD;
  1155. op = ceph_snap(dir) == CEPH_SNAPDIR ?
  1156. CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_GETATTR;
  1157. req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
  1158. if (!IS_ERR(req)) {
  1159. req->r_dentry = dget(dentry);
  1160. req->r_num_caps = op == CEPH_MDS_OP_GETATTR ? 1 : 2;
  1161. mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
  1162. if (ceph_security_xattr_wanted(dir))
  1163. mask |= CEPH_CAP_XATTR_SHARED;
  1164. req->r_args.getattr.mask = cpu_to_le32(mask);
  1165. err = ceph_mdsc_do_request(mdsc, NULL, req);
  1166. switch (err) {
  1167. case 0:
  1168. if (d_really_is_positive(dentry) &&
  1169. d_inode(dentry) == req->r_target_inode)
  1170. valid = 1;
  1171. break;
  1172. case -ENOENT:
  1173. if (d_really_is_negative(dentry))
  1174. valid = 1;
  1175. /* Fallthrough */
  1176. default:
  1177. break;
  1178. }
  1179. ceph_mdsc_put_request(req);
  1180. dout("d_revalidate %p lookup result=%d\n",
  1181. dentry, err);
  1182. }
  1183. }
  1184. dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid");
  1185. if (valid) {
  1186. ceph_dentry_lru_touch(dentry);
  1187. } else {
  1188. ceph_dir_clear_complete(dir);
  1189. }
  1190. if (!(flags & LOOKUP_RCU))
  1191. dput(parent);
  1192. return valid;
  1193. }
  1194. /*
  1195. * Release our ceph_dentry_info.
  1196. */
  1197. static void ceph_d_release(struct dentry *dentry)
  1198. {
  1199. struct ceph_dentry_info *di = ceph_dentry(dentry);
  1200. dout("d_release %p\n", dentry);
  1201. ceph_dentry_lru_del(dentry);
  1202. spin_lock(&dentry->d_lock);
  1203. dentry->d_fsdata = NULL;
  1204. spin_unlock(&dentry->d_lock);
  1205. if (di->lease_session)
  1206. ceph_put_mds_session(di->lease_session);
  1207. kmem_cache_free(ceph_dentry_cachep, di);
  1208. }
  1209. static int ceph_snapdir_d_revalidate(struct dentry *dentry,
  1210. unsigned int flags)
  1211. {
  1212. /*
  1213. * Eventually, we'll want to revalidate snapped metadata
  1214. * too... probably...
  1215. */
  1216. return 1;
  1217. }
  1218. /*
  1219. * When the VFS prunes a dentry from the cache, we need to clear the
  1220. * complete flag on the parent directory.
  1221. *
  1222. * Called under dentry->d_lock.
  1223. */
  1224. static void ceph_d_prune(struct dentry *dentry)
  1225. {
  1226. dout("ceph_d_prune %p\n", dentry);
  1227. /* do we have a valid parent? */
  1228. if (IS_ROOT(dentry))
  1229. return;
  1230. /* if we are not hashed, we don't affect dir's completeness */
  1231. if (d_unhashed(dentry))
  1232. return;
  1233. /*
  1234. * we hold d_lock, so d_parent is stable, and d_fsdata is never
  1235. * cleared until d_release
  1236. */
  1237. ceph_dir_clear_complete(d_inode(dentry->d_parent));
  1238. }
  1239. /*
  1240. * read() on a dir. This weird interface hack only works if mounted
  1241. * with '-o dirstat'.
  1242. */
  1243. static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
  1244. loff_t *ppos)
  1245. {
  1246. struct ceph_file_info *cf = file->private_data;
  1247. struct inode *inode = file_inode(file);
  1248. struct ceph_inode_info *ci = ceph_inode(inode);
  1249. int left;
  1250. const int bufsize = 1024;
  1251. if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT))
  1252. return -EISDIR;
  1253. if (!cf->dir_info) {
  1254. cf->dir_info = kmalloc(bufsize, GFP_KERNEL);
  1255. if (!cf->dir_info)
  1256. return -ENOMEM;
  1257. cf->dir_info_len =
  1258. snprintf(cf->dir_info, bufsize,
  1259. "entries: %20lld\n"
  1260. " files: %20lld\n"
  1261. " subdirs: %20lld\n"
  1262. "rentries: %20lld\n"
  1263. " rfiles: %20lld\n"
  1264. " rsubdirs: %20lld\n"
  1265. "rbytes: %20lld\n"
  1266. "rctime: %10ld.%09ld\n",
  1267. ci->i_files + ci->i_subdirs,
  1268. ci->i_files,
  1269. ci->i_subdirs,
  1270. ci->i_rfiles + ci->i_rsubdirs,
  1271. ci->i_rfiles,
  1272. ci->i_rsubdirs,
  1273. ci->i_rbytes,
  1274. (long)ci->i_rctime.tv_sec,
  1275. (long)ci->i_rctime.tv_nsec);
  1276. }
  1277. if (*ppos >= cf->dir_info_len)
  1278. return 0;
  1279. size = min_t(unsigned, size, cf->dir_info_len-*ppos);
  1280. left = copy_to_user(buf, cf->dir_info + *ppos, size);
  1281. if (left == size)
  1282. return -EFAULT;
  1283. *ppos += (size - left);
  1284. return size - left;
  1285. }
  1286. /*
  1287. * We maintain a private dentry LRU.
  1288. *
  1289. * FIXME: this needs to be changed to a per-mds lru to be useful.
  1290. */
  1291. void ceph_dentry_lru_add(struct dentry *dn)
  1292. {
  1293. struct ceph_dentry_info *di = ceph_dentry(dn);
  1294. struct ceph_mds_client *mdsc;
  1295. dout("dentry_lru_add %p %p '%pd'\n", di, dn, dn);
  1296. mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
  1297. spin_lock(&mdsc->dentry_lru_lock);
  1298. list_add_tail(&di->lru, &mdsc->dentry_lru);
  1299. mdsc->num_dentry++;
  1300. spin_unlock(&mdsc->dentry_lru_lock);
  1301. }
  1302. void ceph_dentry_lru_touch(struct dentry *dn)
  1303. {
  1304. struct ceph_dentry_info *di = ceph_dentry(dn);
  1305. struct ceph_mds_client *mdsc;
  1306. dout("dentry_lru_touch %p %p '%pd' (offset %lld)\n", di, dn, dn,
  1307. di->offset);
  1308. mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
  1309. spin_lock(&mdsc->dentry_lru_lock);
  1310. list_move_tail(&di->lru, &mdsc->dentry_lru);
  1311. spin_unlock(&mdsc->dentry_lru_lock);
  1312. }
  1313. void ceph_dentry_lru_del(struct dentry *dn)
  1314. {
  1315. struct ceph_dentry_info *di = ceph_dentry(dn);
  1316. struct ceph_mds_client *mdsc;
  1317. dout("dentry_lru_del %p %p '%pd'\n", di, dn, dn);
  1318. mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
  1319. spin_lock(&mdsc->dentry_lru_lock);
  1320. list_del_init(&di->lru);
  1321. mdsc->num_dentry--;
  1322. spin_unlock(&mdsc->dentry_lru_lock);
  1323. }
  1324. /*
  1325. * Return name hash for a given dentry. This is dependent on
  1326. * the parent directory's hash function.
  1327. */
  1328. unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
  1329. {
  1330. struct ceph_inode_info *dci = ceph_inode(dir);
  1331. switch (dci->i_dir_layout.dl_dir_hash) {
  1332. case 0: /* for backward compat */
  1333. case CEPH_STR_HASH_LINUX:
  1334. return dn->d_name.hash;
  1335. default:
  1336. return ceph_str_hash(dci->i_dir_layout.dl_dir_hash,
  1337. dn->d_name.name, dn->d_name.len);
  1338. }
  1339. }
  1340. const struct file_operations ceph_dir_fops = {
  1341. .read = ceph_read_dir,
  1342. .iterate = ceph_readdir,
  1343. .llseek = ceph_dir_llseek,
  1344. .open = ceph_open,
  1345. .release = ceph_release,
  1346. .unlocked_ioctl = ceph_ioctl,
  1347. .fsync = ceph_fsync,
  1348. };
  1349. const struct file_operations ceph_snapdir_fops = {
  1350. .iterate = ceph_readdir,
  1351. .llseek = ceph_dir_llseek,
  1352. .open = ceph_open,
  1353. .release = ceph_release,
  1354. };
  1355. const struct inode_operations ceph_dir_iops = {
  1356. .lookup = ceph_lookup,
  1357. .permission = ceph_permission,
  1358. .getattr = ceph_getattr,
  1359. .setattr = ceph_setattr,
  1360. .listxattr = ceph_listxattr,
  1361. .get_acl = ceph_get_acl,
  1362. .set_acl = ceph_set_acl,
  1363. .mknod = ceph_mknod,
  1364. .symlink = ceph_symlink,
  1365. .mkdir = ceph_mkdir,
  1366. .link = ceph_link,
  1367. .unlink = ceph_unlink,
  1368. .rmdir = ceph_unlink,
  1369. .rename = ceph_rename,
  1370. .create = ceph_create,
  1371. .atomic_open = ceph_atomic_open,
  1372. };
  1373. const struct inode_operations ceph_snapdir_iops = {
  1374. .lookup = ceph_lookup,
  1375. .permission = ceph_permission,
  1376. .getattr = ceph_getattr,
  1377. .mkdir = ceph_mkdir,
  1378. .rmdir = ceph_unlink,
  1379. .rename = ceph_rename,
  1380. };
  1381. const struct dentry_operations ceph_dentry_ops = {
  1382. .d_revalidate = ceph_d_revalidate,
  1383. .d_release = ceph_d_release,
  1384. .d_prune = ceph_d_prune,
  1385. };
  1386. const struct dentry_operations ceph_snapdir_dentry_ops = {
  1387. .d_revalidate = ceph_snapdir_d_revalidate,
  1388. .d_release = ceph_d_release,
  1389. };
  1390. const struct dentry_operations ceph_snap_dentry_ops = {
  1391. .d_release = ceph_d_release,
  1392. .d_prune = ceph_d_prune,
  1393. };