dir.c 36 KB

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