dir.c 33 KB

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