dir.c 41 KB

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