dir.c 35 KB

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