dir.c 34 KB

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