dir.c 37 KB

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