dir.c 36 KB

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