dir.c 33 KB

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