dir.c 35 KB

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