dir.c 36 KB

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