dir.c 34 KB

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