dir.c 35 KB

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