dir.c 34 KB

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