dir.c 34 KB

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