dir.c 36 KB

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