inode.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706
  1. #include "ceph_debug.h"
  2. #include <linux/module.h>
  3. #include <linux/fs.h>
  4. #include <linux/smp_lock.h>
  5. #include <linux/slab.h>
  6. #include <linux/string.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/kernel.h>
  9. #include <linux/namei.h>
  10. #include <linux/writeback.h>
  11. #include <linux/vmalloc.h>
  12. #include "super.h"
  13. #include "decode.h"
  14. /*
  15. * Ceph inode operations
  16. *
  17. * Implement basic inode helpers (get, alloc) and inode ops (getattr,
  18. * setattr, etc.), xattr helpers, and helpers for assimilating
  19. * metadata returned by the MDS into our cache.
  20. *
  21. * Also define helpers for doing asynchronous writeback, invalidation,
  22. * and truncation for the benefit of those who can't afford to block
  23. * (typically because they are in the message handler path).
  24. */
  25. static const struct inode_operations ceph_symlink_iops;
  26. static void ceph_invalidate_work(struct work_struct *work);
  27. static void ceph_writeback_work(struct work_struct *work);
  28. static void ceph_vmtruncate_work(struct work_struct *work);
  29. /*
  30. * find or create an inode, given the ceph ino number
  31. */
  32. struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino)
  33. {
  34. struct inode *inode;
  35. ino_t t = ceph_vino_to_ino(vino);
  36. inode = iget5_locked(sb, t, ceph_ino_compare, ceph_set_ino_cb, &vino);
  37. if (inode == NULL)
  38. return ERR_PTR(-ENOMEM);
  39. if (inode->i_state & I_NEW) {
  40. dout("get_inode created new inode %p %llx.%llx ino %llx\n",
  41. inode, ceph_vinop(inode), (u64)inode->i_ino);
  42. unlock_new_inode(inode);
  43. }
  44. dout("get_inode on %lu=%llx.%llx got %p\n", inode->i_ino, vino.ino,
  45. vino.snap, inode);
  46. return inode;
  47. }
  48. /*
  49. * get/constuct snapdir inode for a given directory
  50. */
  51. struct inode *ceph_get_snapdir(struct inode *parent)
  52. {
  53. struct ceph_vino vino = {
  54. .ino = ceph_ino(parent),
  55. .snap = CEPH_SNAPDIR,
  56. };
  57. struct inode *inode = ceph_get_inode(parent->i_sb, vino);
  58. struct ceph_inode_info *ci = ceph_inode(inode);
  59. BUG_ON(!S_ISDIR(parent->i_mode));
  60. if (IS_ERR(inode))
  61. return ERR_PTR(PTR_ERR(inode));
  62. inode->i_mode = parent->i_mode;
  63. inode->i_uid = parent->i_uid;
  64. inode->i_gid = parent->i_gid;
  65. inode->i_op = &ceph_dir_iops;
  66. inode->i_fop = &ceph_dir_fops;
  67. ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
  68. ci->i_rbytes = 0;
  69. return inode;
  70. }
  71. const struct inode_operations ceph_file_iops = {
  72. .permission = ceph_permission,
  73. .setattr = ceph_setattr,
  74. .getattr = ceph_getattr,
  75. .setxattr = ceph_setxattr,
  76. .getxattr = ceph_getxattr,
  77. .listxattr = ceph_listxattr,
  78. .removexattr = ceph_removexattr,
  79. };
  80. /*
  81. * We use a 'frag tree' to keep track of the MDS's directory fragments
  82. * for a given inode (usually there is just a single fragment). We
  83. * need to know when a child frag is delegated to a new MDS, or when
  84. * it is flagged as replicated, so we can direct our requests
  85. * accordingly.
  86. */
  87. /*
  88. * find/create a frag in the tree
  89. */
  90. static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
  91. u32 f)
  92. {
  93. struct rb_node **p;
  94. struct rb_node *parent = NULL;
  95. struct ceph_inode_frag *frag;
  96. int c;
  97. p = &ci->i_fragtree.rb_node;
  98. while (*p) {
  99. parent = *p;
  100. frag = rb_entry(parent, struct ceph_inode_frag, node);
  101. c = ceph_frag_compare(f, frag->frag);
  102. if (c < 0)
  103. p = &(*p)->rb_left;
  104. else if (c > 0)
  105. p = &(*p)->rb_right;
  106. else
  107. return frag;
  108. }
  109. frag = kmalloc(sizeof(*frag), GFP_NOFS);
  110. if (!frag) {
  111. pr_err("__get_or_create_frag ENOMEM on %p %llx.%llx "
  112. "frag %x\n", &ci->vfs_inode,
  113. ceph_vinop(&ci->vfs_inode), f);
  114. return ERR_PTR(-ENOMEM);
  115. }
  116. frag->frag = f;
  117. frag->split_by = 0;
  118. frag->mds = -1;
  119. frag->ndist = 0;
  120. rb_link_node(&frag->node, parent, p);
  121. rb_insert_color(&frag->node, &ci->i_fragtree);
  122. dout("get_or_create_frag added %llx.%llx frag %x\n",
  123. ceph_vinop(&ci->vfs_inode), f);
  124. return frag;
  125. }
  126. /*
  127. * find a specific frag @f
  128. */
  129. struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
  130. {
  131. struct rb_node *n = ci->i_fragtree.rb_node;
  132. while (n) {
  133. struct ceph_inode_frag *frag =
  134. rb_entry(n, struct ceph_inode_frag, node);
  135. int c = ceph_frag_compare(f, frag->frag);
  136. if (c < 0)
  137. n = n->rb_left;
  138. else if (c > 0)
  139. n = n->rb_right;
  140. else
  141. return frag;
  142. }
  143. return NULL;
  144. }
  145. /*
  146. * Choose frag containing the given value @v. If @pfrag is
  147. * specified, copy the frag delegation info to the caller if
  148. * it is present.
  149. */
  150. u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
  151. struct ceph_inode_frag *pfrag,
  152. int *found)
  153. {
  154. u32 t = ceph_frag_make(0, 0);
  155. struct ceph_inode_frag *frag;
  156. unsigned nway, i;
  157. u32 n;
  158. if (found)
  159. *found = 0;
  160. mutex_lock(&ci->i_fragtree_mutex);
  161. while (1) {
  162. WARN_ON(!ceph_frag_contains_value(t, v));
  163. frag = __ceph_find_frag(ci, t);
  164. if (!frag)
  165. break; /* t is a leaf */
  166. if (frag->split_by == 0) {
  167. if (pfrag)
  168. memcpy(pfrag, frag, sizeof(*pfrag));
  169. if (found)
  170. *found = 1;
  171. break;
  172. }
  173. /* choose child */
  174. nway = 1 << frag->split_by;
  175. dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t,
  176. frag->split_by, nway);
  177. for (i = 0; i < nway; i++) {
  178. n = ceph_frag_make_child(t, frag->split_by, i);
  179. if (ceph_frag_contains_value(n, v)) {
  180. t = n;
  181. break;
  182. }
  183. }
  184. BUG_ON(i == nway);
  185. }
  186. dout("choose_frag(%x) = %x\n", v, t);
  187. mutex_unlock(&ci->i_fragtree_mutex);
  188. return t;
  189. }
  190. /*
  191. * Process dirfrag (delegation) info from the mds. Include leaf
  192. * fragment in tree ONLY if ndist > 0. Otherwise, only
  193. * branches/splits are included in i_fragtree)
  194. */
  195. static int ceph_fill_dirfrag(struct inode *inode,
  196. struct ceph_mds_reply_dirfrag *dirinfo)
  197. {
  198. struct ceph_inode_info *ci = ceph_inode(inode);
  199. struct ceph_inode_frag *frag;
  200. u32 id = le32_to_cpu(dirinfo->frag);
  201. int mds = le32_to_cpu(dirinfo->auth);
  202. int ndist = le32_to_cpu(dirinfo->ndist);
  203. int i;
  204. int err = 0;
  205. mutex_lock(&ci->i_fragtree_mutex);
  206. if (ndist == 0) {
  207. /* no delegation info needed. */
  208. frag = __ceph_find_frag(ci, id);
  209. if (!frag)
  210. goto out;
  211. if (frag->split_by == 0) {
  212. /* tree leaf, remove */
  213. dout("fill_dirfrag removed %llx.%llx frag %x"
  214. " (no ref)\n", ceph_vinop(inode), id);
  215. rb_erase(&frag->node, &ci->i_fragtree);
  216. kfree(frag);
  217. } else {
  218. /* tree branch, keep and clear */
  219. dout("fill_dirfrag cleared %llx.%llx frag %x"
  220. " referral\n", ceph_vinop(inode), id);
  221. frag->mds = -1;
  222. frag->ndist = 0;
  223. }
  224. goto out;
  225. }
  226. /* find/add this frag to store mds delegation info */
  227. frag = __get_or_create_frag(ci, id);
  228. if (IS_ERR(frag)) {
  229. /* this is not the end of the world; we can continue
  230. with bad/inaccurate delegation info */
  231. pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n",
  232. ceph_vinop(inode), le32_to_cpu(dirinfo->frag));
  233. err = -ENOMEM;
  234. goto out;
  235. }
  236. frag->mds = mds;
  237. frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
  238. for (i = 0; i < frag->ndist; i++)
  239. frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
  240. dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n",
  241. ceph_vinop(inode), frag->frag, frag->ndist);
  242. out:
  243. mutex_unlock(&ci->i_fragtree_mutex);
  244. return err;
  245. }
  246. /*
  247. * initialize a newly allocated inode.
  248. */
  249. struct inode *ceph_alloc_inode(struct super_block *sb)
  250. {
  251. struct ceph_inode_info *ci;
  252. int i;
  253. ci = kmem_cache_alloc(ceph_inode_cachep, GFP_NOFS);
  254. if (!ci)
  255. return NULL;
  256. dout("alloc_inode %p\n", &ci->vfs_inode);
  257. ci->i_version = 0;
  258. ci->i_time_warp_seq = 0;
  259. ci->i_ceph_flags = 0;
  260. ci->i_release_count = 0;
  261. ci->i_symlink = NULL;
  262. ci->i_fragtree = RB_ROOT;
  263. mutex_init(&ci->i_fragtree_mutex);
  264. ci->i_xattrs.blob = NULL;
  265. ci->i_xattrs.prealloc_blob = NULL;
  266. ci->i_xattrs.dirty = false;
  267. ci->i_xattrs.index = RB_ROOT;
  268. ci->i_xattrs.count = 0;
  269. ci->i_xattrs.names_size = 0;
  270. ci->i_xattrs.vals_size = 0;
  271. ci->i_xattrs.version = 0;
  272. ci->i_xattrs.index_version = 0;
  273. ci->i_caps = RB_ROOT;
  274. ci->i_auth_cap = NULL;
  275. ci->i_dirty_caps = 0;
  276. ci->i_flushing_caps = 0;
  277. INIT_LIST_HEAD(&ci->i_dirty_item);
  278. INIT_LIST_HEAD(&ci->i_flushing_item);
  279. ci->i_cap_flush_seq = 0;
  280. ci->i_cap_flush_last_tid = 0;
  281. memset(&ci->i_cap_flush_tid, 0, sizeof(ci->i_cap_flush_tid));
  282. init_waitqueue_head(&ci->i_cap_wq);
  283. ci->i_hold_caps_min = 0;
  284. ci->i_hold_caps_max = 0;
  285. INIT_LIST_HEAD(&ci->i_cap_delay_list);
  286. ci->i_cap_exporting_mds = 0;
  287. ci->i_cap_exporting_mseq = 0;
  288. ci->i_cap_exporting_issued = 0;
  289. INIT_LIST_HEAD(&ci->i_cap_snaps);
  290. ci->i_head_snapc = NULL;
  291. ci->i_snap_caps = 0;
  292. for (i = 0; i < CEPH_FILE_MODE_NUM; i++)
  293. ci->i_nr_by_mode[i] = 0;
  294. ci->i_truncate_seq = 0;
  295. ci->i_truncate_size = 0;
  296. ci->i_truncate_pending = 0;
  297. ci->i_max_size = 0;
  298. ci->i_reported_size = 0;
  299. ci->i_wanted_max_size = 0;
  300. ci->i_requested_max_size = 0;
  301. ci->i_pin_ref = 0;
  302. ci->i_rd_ref = 0;
  303. ci->i_rdcache_ref = 0;
  304. ci->i_wr_ref = 0;
  305. ci->i_wrbuffer_ref = 0;
  306. ci->i_wrbuffer_ref_head = 0;
  307. ci->i_shared_gen = 0;
  308. ci->i_rdcache_gen = 0;
  309. ci->i_rdcache_revoking = 0;
  310. INIT_LIST_HEAD(&ci->i_unsafe_writes);
  311. INIT_LIST_HEAD(&ci->i_unsafe_dirops);
  312. spin_lock_init(&ci->i_unsafe_lock);
  313. ci->i_snap_realm = NULL;
  314. INIT_LIST_HEAD(&ci->i_snap_realm_item);
  315. INIT_LIST_HEAD(&ci->i_snap_flush_item);
  316. INIT_WORK(&ci->i_wb_work, ceph_writeback_work);
  317. INIT_WORK(&ci->i_pg_inv_work, ceph_invalidate_work);
  318. INIT_WORK(&ci->i_vmtruncate_work, ceph_vmtruncate_work);
  319. return &ci->vfs_inode;
  320. }
  321. void ceph_destroy_inode(struct inode *inode)
  322. {
  323. struct ceph_inode_info *ci = ceph_inode(inode);
  324. struct ceph_inode_frag *frag;
  325. struct rb_node *n;
  326. dout("destroy_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
  327. ceph_queue_caps_release(inode);
  328. kfree(ci->i_symlink);
  329. while ((n = rb_first(&ci->i_fragtree)) != NULL) {
  330. frag = rb_entry(n, struct ceph_inode_frag, node);
  331. rb_erase(n, &ci->i_fragtree);
  332. kfree(frag);
  333. }
  334. __ceph_destroy_xattrs(ci);
  335. if (ci->i_xattrs.blob)
  336. ceph_buffer_put(ci->i_xattrs.blob);
  337. if (ci->i_xattrs.prealloc_blob)
  338. ceph_buffer_put(ci->i_xattrs.prealloc_blob);
  339. kmem_cache_free(ceph_inode_cachep, ci);
  340. }
  341. /*
  342. * Helpers to fill in size, ctime, mtime, and atime. We have to be
  343. * careful because either the client or MDS may have more up to date
  344. * info, depending on which capabilities are held, and whether
  345. * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
  346. * and size are monotonically increasing, except when utimes() or
  347. * truncate() increments the corresponding _seq values.)
  348. */
  349. int ceph_fill_file_size(struct inode *inode, int issued,
  350. u32 truncate_seq, u64 truncate_size, u64 size)
  351. {
  352. struct ceph_inode_info *ci = ceph_inode(inode);
  353. int queue_trunc = 0;
  354. if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
  355. (truncate_seq == ci->i_truncate_seq && size > inode->i_size)) {
  356. dout("size %lld -> %llu\n", inode->i_size, size);
  357. inode->i_size = size;
  358. inode->i_blocks = (size + (1<<9) - 1) >> 9;
  359. ci->i_reported_size = size;
  360. if (truncate_seq != ci->i_truncate_seq) {
  361. dout("truncate_seq %u -> %u\n",
  362. ci->i_truncate_seq, truncate_seq);
  363. ci->i_truncate_seq = truncate_seq;
  364. /*
  365. * If we hold relevant caps, or in the case where we're
  366. * not the only client referencing this file and we
  367. * don't hold those caps, then we need to check whether
  368. * the file is either opened or mmaped
  369. */
  370. if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_RD|
  371. CEPH_CAP_FILE_WR|CEPH_CAP_FILE_BUFFER|
  372. CEPH_CAP_FILE_EXCL)) ||
  373. mapping_mapped(inode->i_mapping) ||
  374. __ceph_caps_file_wanted(ci)) {
  375. ci->i_truncate_pending++;
  376. queue_trunc = 1;
  377. }
  378. }
  379. }
  380. if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
  381. ci->i_truncate_size != truncate_size) {
  382. dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
  383. truncate_size);
  384. ci->i_truncate_size = truncate_size;
  385. }
  386. return queue_trunc;
  387. }
  388. void ceph_fill_file_time(struct inode *inode, int issued,
  389. u64 time_warp_seq, struct timespec *ctime,
  390. struct timespec *mtime, struct timespec *atime)
  391. {
  392. struct ceph_inode_info *ci = ceph_inode(inode);
  393. int warn = 0;
  394. if (issued & (CEPH_CAP_FILE_EXCL|
  395. CEPH_CAP_FILE_WR|
  396. CEPH_CAP_FILE_BUFFER)) {
  397. if (timespec_compare(ctime, &inode->i_ctime) > 0) {
  398. dout("ctime %ld.%09ld -> %ld.%09ld inc w/ cap\n",
  399. inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
  400. ctime->tv_sec, ctime->tv_nsec);
  401. inode->i_ctime = *ctime;
  402. }
  403. if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
  404. /* the MDS did a utimes() */
  405. dout("mtime %ld.%09ld -> %ld.%09ld "
  406. "tw %d -> %d\n",
  407. inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
  408. mtime->tv_sec, mtime->tv_nsec,
  409. ci->i_time_warp_seq, (int)time_warp_seq);
  410. inode->i_mtime = *mtime;
  411. inode->i_atime = *atime;
  412. ci->i_time_warp_seq = time_warp_seq;
  413. } else if (time_warp_seq == ci->i_time_warp_seq) {
  414. /* nobody did utimes(); take the max */
  415. if (timespec_compare(mtime, &inode->i_mtime) > 0) {
  416. dout("mtime %ld.%09ld -> %ld.%09ld inc\n",
  417. inode->i_mtime.tv_sec,
  418. inode->i_mtime.tv_nsec,
  419. mtime->tv_sec, mtime->tv_nsec);
  420. inode->i_mtime = *mtime;
  421. }
  422. if (timespec_compare(atime, &inode->i_atime) > 0) {
  423. dout("atime %ld.%09ld -> %ld.%09ld inc\n",
  424. inode->i_atime.tv_sec,
  425. inode->i_atime.tv_nsec,
  426. atime->tv_sec, atime->tv_nsec);
  427. inode->i_atime = *atime;
  428. }
  429. } else if (issued & CEPH_CAP_FILE_EXCL) {
  430. /* we did a utimes(); ignore mds values */
  431. } else {
  432. warn = 1;
  433. }
  434. } else {
  435. /* we have no write caps; whatever the MDS says is true */
  436. if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
  437. inode->i_ctime = *ctime;
  438. inode->i_mtime = *mtime;
  439. inode->i_atime = *atime;
  440. ci->i_time_warp_seq = time_warp_seq;
  441. } else {
  442. warn = 1;
  443. }
  444. }
  445. if (warn) /* time_warp_seq shouldn't go backwards */
  446. dout("%p mds time_warp_seq %llu < %u\n",
  447. inode, time_warp_seq, ci->i_time_warp_seq);
  448. }
  449. /*
  450. * Populate an inode based on info from mds. May be called on new or
  451. * existing inodes.
  452. */
  453. static int fill_inode(struct inode *inode,
  454. struct ceph_mds_reply_info_in *iinfo,
  455. struct ceph_mds_reply_dirfrag *dirinfo,
  456. struct ceph_mds_session *session,
  457. unsigned long ttl_from, int cap_fmode,
  458. struct ceph_cap_reservation *caps_reservation)
  459. {
  460. struct ceph_mds_reply_inode *info = iinfo->in;
  461. struct ceph_inode_info *ci = ceph_inode(inode);
  462. int i;
  463. int issued, implemented;
  464. struct timespec mtime, atime, ctime;
  465. u32 nsplits;
  466. struct ceph_buffer *xattr_blob = NULL;
  467. int err = 0;
  468. int queue_trunc = 0;
  469. dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
  470. inode, ceph_vinop(inode), le64_to_cpu(info->version),
  471. ci->i_version);
  472. /*
  473. * prealloc xattr data, if it looks like we'll need it. only
  474. * if len > 4 (meaning there are actually xattrs; the first 4
  475. * bytes are the xattr count).
  476. */
  477. if (iinfo->xattr_len > 4) {
  478. xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
  479. if (!xattr_blob)
  480. pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
  481. iinfo->xattr_len);
  482. }
  483. spin_lock(&inode->i_lock);
  484. /*
  485. * provided version will be odd if inode value is projected,
  486. * even if stable. skip the update if we have a newer info
  487. * (e.g., due to inode info racing form multiple MDSs), or if
  488. * we are getting projected (unstable) inode info.
  489. */
  490. if (le64_to_cpu(info->version) > 0 &&
  491. (ci->i_version & ~1) > le64_to_cpu(info->version))
  492. goto no_change;
  493. issued = __ceph_caps_issued(ci, &implemented);
  494. issued |= implemented | __ceph_caps_dirty(ci);
  495. /* update inode */
  496. ci->i_version = le64_to_cpu(info->version);
  497. inode->i_version++;
  498. inode->i_rdev = le32_to_cpu(info->rdev);
  499. if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
  500. inode->i_mode = le32_to_cpu(info->mode);
  501. inode->i_uid = le32_to_cpu(info->uid);
  502. inode->i_gid = le32_to_cpu(info->gid);
  503. dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
  504. inode->i_uid, inode->i_gid);
  505. }
  506. if ((issued & CEPH_CAP_LINK_EXCL) == 0)
  507. inode->i_nlink = le32_to_cpu(info->nlink);
  508. /* be careful with mtime, atime, size */
  509. ceph_decode_timespec(&atime, &info->atime);
  510. ceph_decode_timespec(&mtime, &info->mtime);
  511. ceph_decode_timespec(&ctime, &info->ctime);
  512. queue_trunc = ceph_fill_file_size(inode, issued,
  513. le32_to_cpu(info->truncate_seq),
  514. le64_to_cpu(info->truncate_size),
  515. le64_to_cpu(info->size));
  516. ceph_fill_file_time(inode, issued,
  517. le32_to_cpu(info->time_warp_seq),
  518. &ctime, &mtime, &atime);
  519. ci->i_max_size = le64_to_cpu(info->max_size);
  520. ci->i_layout = info->layout;
  521. inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
  522. /* xattrs */
  523. /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
  524. if ((issued & CEPH_CAP_XATTR_EXCL) == 0 &&
  525. le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
  526. if (ci->i_xattrs.blob)
  527. ceph_buffer_put(ci->i_xattrs.blob);
  528. ci->i_xattrs.blob = xattr_blob;
  529. if (xattr_blob)
  530. memcpy(ci->i_xattrs.blob->vec.iov_base,
  531. iinfo->xattr_data, iinfo->xattr_len);
  532. ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
  533. }
  534. inode->i_mapping->a_ops = &ceph_aops;
  535. inode->i_mapping->backing_dev_info =
  536. &ceph_client(inode->i_sb)->backing_dev_info;
  537. switch (inode->i_mode & S_IFMT) {
  538. case S_IFIFO:
  539. case S_IFBLK:
  540. case S_IFCHR:
  541. case S_IFSOCK:
  542. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  543. inode->i_op = &ceph_file_iops;
  544. break;
  545. case S_IFREG:
  546. inode->i_op = &ceph_file_iops;
  547. inode->i_fop = &ceph_file_fops;
  548. break;
  549. case S_IFLNK:
  550. inode->i_op = &ceph_symlink_iops;
  551. if (!ci->i_symlink) {
  552. int symlen = iinfo->symlink_len;
  553. char *sym;
  554. BUG_ON(symlen != inode->i_size);
  555. spin_unlock(&inode->i_lock);
  556. err = -ENOMEM;
  557. sym = kmalloc(symlen+1, GFP_NOFS);
  558. if (!sym)
  559. goto out;
  560. memcpy(sym, iinfo->symlink, symlen);
  561. sym[symlen] = 0;
  562. spin_lock(&inode->i_lock);
  563. if (!ci->i_symlink)
  564. ci->i_symlink = sym;
  565. else
  566. kfree(sym); /* lost a race */
  567. }
  568. break;
  569. case S_IFDIR:
  570. inode->i_op = &ceph_dir_iops;
  571. inode->i_fop = &ceph_dir_fops;
  572. ci->i_files = le64_to_cpu(info->files);
  573. ci->i_subdirs = le64_to_cpu(info->subdirs);
  574. ci->i_rbytes = le64_to_cpu(info->rbytes);
  575. ci->i_rfiles = le64_to_cpu(info->rfiles);
  576. ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
  577. ceph_decode_timespec(&ci->i_rctime, &info->rctime);
  578. /* set dir completion flag? */
  579. if (ci->i_files == 0 && ci->i_subdirs == 0 &&
  580. ceph_snap(inode) == CEPH_NOSNAP &&
  581. (le32_to_cpu(info->cap.caps) & CEPH_CAP_FILE_SHARED)) {
  582. dout(" marking %p complete (empty)\n", inode);
  583. ci->i_ceph_flags |= CEPH_I_COMPLETE;
  584. ci->i_max_offset = 2;
  585. }
  586. /* it may be better to set st_size in getattr instead? */
  587. if (ceph_test_opt(ceph_client(inode->i_sb), RBYTES))
  588. inode->i_size = ci->i_rbytes;
  589. break;
  590. default:
  591. pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
  592. ceph_vinop(inode), inode->i_mode);
  593. }
  594. no_change:
  595. spin_unlock(&inode->i_lock);
  596. /* queue truncate if we saw i_size decrease */
  597. if (queue_trunc)
  598. ceph_queue_vmtruncate(inode);
  599. /* populate frag tree */
  600. /* FIXME: move me up, if/when version reflects fragtree changes */
  601. nsplits = le32_to_cpu(info->fragtree.nsplits);
  602. mutex_lock(&ci->i_fragtree_mutex);
  603. for (i = 0; i < nsplits; i++) {
  604. u32 id = le32_to_cpu(info->fragtree.splits[i].frag);
  605. struct ceph_inode_frag *frag = __get_or_create_frag(ci, id);
  606. if (IS_ERR(frag))
  607. continue;
  608. frag->split_by = le32_to_cpu(info->fragtree.splits[i].by);
  609. dout(" frag %x split by %d\n", frag->frag, frag->split_by);
  610. }
  611. mutex_unlock(&ci->i_fragtree_mutex);
  612. /* were we issued a capability? */
  613. if (info->cap.caps) {
  614. if (ceph_snap(inode) == CEPH_NOSNAP) {
  615. ceph_add_cap(inode, session,
  616. le64_to_cpu(info->cap.cap_id),
  617. cap_fmode,
  618. le32_to_cpu(info->cap.caps),
  619. le32_to_cpu(info->cap.wanted),
  620. le32_to_cpu(info->cap.seq),
  621. le32_to_cpu(info->cap.mseq),
  622. le64_to_cpu(info->cap.realm),
  623. info->cap.flags,
  624. caps_reservation);
  625. } else {
  626. spin_lock(&inode->i_lock);
  627. dout(" %p got snap_caps %s\n", inode,
  628. ceph_cap_string(le32_to_cpu(info->cap.caps)));
  629. ci->i_snap_caps |= le32_to_cpu(info->cap.caps);
  630. if (cap_fmode >= 0)
  631. __ceph_get_fmode(ci, cap_fmode);
  632. spin_unlock(&inode->i_lock);
  633. }
  634. }
  635. /* update delegation info? */
  636. if (dirinfo)
  637. ceph_fill_dirfrag(inode, dirinfo);
  638. err = 0;
  639. out:
  640. if (xattr_blob)
  641. ceph_buffer_put(xattr_blob);
  642. return err;
  643. }
  644. /*
  645. * caller should hold session s_mutex.
  646. */
  647. static void update_dentry_lease(struct dentry *dentry,
  648. struct ceph_mds_reply_lease *lease,
  649. struct ceph_mds_session *session,
  650. unsigned long from_time)
  651. {
  652. struct ceph_dentry_info *di = ceph_dentry(dentry);
  653. long unsigned duration = le32_to_cpu(lease->duration_ms);
  654. long unsigned ttl = from_time + (duration * HZ) / 1000;
  655. long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
  656. struct inode *dir;
  657. /* only track leases on regular dentries */
  658. if (dentry->d_op != &ceph_dentry_ops)
  659. return;
  660. spin_lock(&dentry->d_lock);
  661. dout("update_dentry_lease %p mask %d duration %lu ms ttl %lu\n",
  662. dentry, le16_to_cpu(lease->mask), duration, ttl);
  663. /* make lease_rdcache_gen match directory */
  664. dir = dentry->d_parent->d_inode;
  665. di->lease_shared_gen = ceph_inode(dir)->i_shared_gen;
  666. if (lease->mask == 0)
  667. goto out_unlock;
  668. if (di->lease_gen == session->s_cap_gen &&
  669. time_before(ttl, dentry->d_time))
  670. goto out_unlock; /* we already have a newer lease. */
  671. if (di->lease_session && di->lease_session != session)
  672. goto out_unlock;
  673. ceph_dentry_lru_touch(dentry);
  674. if (!di->lease_session)
  675. di->lease_session = ceph_get_mds_session(session);
  676. di->lease_gen = session->s_cap_gen;
  677. di->lease_seq = le32_to_cpu(lease->seq);
  678. di->lease_renew_after = half_ttl;
  679. di->lease_renew_from = 0;
  680. dentry->d_time = ttl;
  681. out_unlock:
  682. spin_unlock(&dentry->d_lock);
  683. return;
  684. }
  685. /*
  686. * splice a dentry to an inode.
  687. * caller must hold directory i_mutex for this to be safe.
  688. *
  689. * we will only rehash the resulting dentry if @prehash is
  690. * true; @prehash will be set to false (for the benefit of
  691. * the caller) if we fail.
  692. */
  693. static struct dentry *splice_dentry(struct dentry *dn, struct inode *in,
  694. bool *prehash)
  695. {
  696. struct dentry *realdn;
  697. /* dn must be unhashed */
  698. if (!d_unhashed(dn))
  699. d_drop(dn);
  700. realdn = d_materialise_unique(dn, in);
  701. if (IS_ERR(realdn)) {
  702. pr_err("splice_dentry error %p inode %p ino %llx.%llx\n",
  703. dn, in, ceph_vinop(in));
  704. if (prehash)
  705. *prehash = false; /* don't rehash on error */
  706. dn = realdn; /* note realdn contains the error */
  707. goto out;
  708. } else if (realdn) {
  709. dout("dn %p (%d) spliced with %p (%d) "
  710. "inode %p ino %llx.%llx\n",
  711. dn, atomic_read(&dn->d_count),
  712. realdn, atomic_read(&realdn->d_count),
  713. realdn->d_inode, ceph_vinop(realdn->d_inode));
  714. dput(dn);
  715. dn = realdn;
  716. } else {
  717. BUG_ON(!ceph_dentry(dn));
  718. dout("dn %p attached to %p ino %llx.%llx\n",
  719. dn, dn->d_inode, ceph_vinop(dn->d_inode));
  720. }
  721. if ((!prehash || *prehash) && d_unhashed(dn))
  722. d_rehash(dn);
  723. out:
  724. return dn;
  725. }
  726. /*
  727. * Set dentry's directory position based on the current dir's max, and
  728. * order it in d_subdirs, so that dcache_readdir behaves.
  729. */
  730. static void ceph_set_dentry_offset(struct dentry *dn)
  731. {
  732. struct dentry *dir = dn->d_parent;
  733. struct inode *inode = dn->d_parent->d_inode;
  734. struct ceph_dentry_info *di;
  735. BUG_ON(!inode);
  736. di = ceph_dentry(dn);
  737. spin_lock(&inode->i_lock);
  738. di->offset = ceph_inode(inode)->i_max_offset++;
  739. spin_unlock(&inode->i_lock);
  740. spin_lock(&dcache_lock);
  741. spin_lock(&dn->d_lock);
  742. list_move_tail(&dir->d_subdirs, &dn->d_u.d_child);
  743. dout("set_dentry_offset %p %lld (%p %p)\n", dn, di->offset,
  744. dn->d_u.d_child.prev, dn->d_u.d_child.next);
  745. spin_unlock(&dn->d_lock);
  746. spin_unlock(&dcache_lock);
  747. }
  748. /*
  749. * Incorporate results into the local cache. This is either just
  750. * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
  751. * after a lookup).
  752. *
  753. * A reply may contain
  754. * a directory inode along with a dentry.
  755. * and/or a target inode
  756. *
  757. * Called with snap_rwsem (read).
  758. */
  759. int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req,
  760. struct ceph_mds_session *session)
  761. {
  762. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  763. struct inode *in = NULL;
  764. struct ceph_mds_reply_inode *ininfo;
  765. struct ceph_vino vino;
  766. int i = 0;
  767. int err = 0;
  768. dout("fill_trace %p is_dentry %d is_target %d\n", req,
  769. rinfo->head->is_dentry, rinfo->head->is_target);
  770. #if 0
  771. /*
  772. * Debugging hook:
  773. *
  774. * If we resend completed ops to a recovering mds, we get no
  775. * trace. Since that is very rare, pretend this is the case
  776. * to ensure the 'no trace' handlers in the callers behave.
  777. *
  778. * Fill in inodes unconditionally to avoid breaking cap
  779. * invariants.
  780. */
  781. if (rinfo->head->op & CEPH_MDS_OP_WRITE) {
  782. pr_info("fill_trace faking empty trace on %lld %s\n",
  783. req->r_tid, ceph_mds_op_name(rinfo->head->op));
  784. if (rinfo->head->is_dentry) {
  785. rinfo->head->is_dentry = 0;
  786. err = fill_inode(req->r_locked_dir,
  787. &rinfo->diri, rinfo->dirfrag,
  788. session, req->r_request_started, -1);
  789. }
  790. if (rinfo->head->is_target) {
  791. rinfo->head->is_target = 0;
  792. ininfo = rinfo->targeti.in;
  793. vino.ino = le64_to_cpu(ininfo->ino);
  794. vino.snap = le64_to_cpu(ininfo->snapid);
  795. in = ceph_get_inode(sb, vino);
  796. err = fill_inode(in, &rinfo->targeti, NULL,
  797. session, req->r_request_started,
  798. req->r_fmode);
  799. iput(in);
  800. }
  801. }
  802. #endif
  803. if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
  804. dout("fill_trace reply is empty!\n");
  805. if (rinfo->head->result == 0 && req->r_locked_dir) {
  806. struct ceph_inode_info *ci =
  807. ceph_inode(req->r_locked_dir);
  808. dout(" clearing %p complete (empty trace)\n",
  809. req->r_locked_dir);
  810. ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
  811. ci->i_release_count++;
  812. }
  813. return 0;
  814. }
  815. if (rinfo->head->is_dentry) {
  816. struct inode *dir = req->r_locked_dir;
  817. err = fill_inode(dir, &rinfo->diri, rinfo->dirfrag,
  818. session, req->r_request_started, -1,
  819. &req->r_caps_reservation);
  820. if (err < 0)
  821. return err;
  822. }
  823. if (rinfo->head->is_dentry && !req->r_aborted) {
  824. /*
  825. * lookup link rename : null -> possibly existing inode
  826. * mknod symlink mkdir : null -> new inode
  827. * unlink : linked -> null
  828. */
  829. struct inode *dir = req->r_locked_dir;
  830. struct dentry *dn = req->r_dentry;
  831. bool have_dir_cap, have_lease;
  832. BUG_ON(!dn);
  833. BUG_ON(!dir);
  834. BUG_ON(dn->d_parent->d_inode != dir);
  835. BUG_ON(ceph_ino(dir) !=
  836. le64_to_cpu(rinfo->diri.in->ino));
  837. BUG_ON(ceph_snap(dir) !=
  838. le64_to_cpu(rinfo->diri.in->snapid));
  839. /* do we have a lease on the whole dir? */
  840. have_dir_cap =
  841. (le32_to_cpu(rinfo->diri.in->cap.caps) &
  842. CEPH_CAP_FILE_SHARED);
  843. /* do we have a dn lease? */
  844. have_lease = have_dir_cap ||
  845. (le16_to_cpu(rinfo->dlease->mask) &
  846. CEPH_LOCK_DN);
  847. if (!have_lease)
  848. dout("fill_trace no dentry lease or dir cap\n");
  849. /* rename? */
  850. if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
  851. dout(" src %p '%.*s' dst %p '%.*s'\n",
  852. req->r_old_dentry,
  853. req->r_old_dentry->d_name.len,
  854. req->r_old_dentry->d_name.name,
  855. dn, dn->d_name.len, dn->d_name.name);
  856. dout("fill_trace doing d_move %p -> %p\n",
  857. req->r_old_dentry, dn);
  858. d_move(req->r_old_dentry, dn);
  859. dout(" src %p '%.*s' dst %p '%.*s'\n",
  860. req->r_old_dentry,
  861. req->r_old_dentry->d_name.len,
  862. req->r_old_dentry->d_name.name,
  863. dn, dn->d_name.len, dn->d_name.name);
  864. /* ensure target dentry is invalidated, despite
  865. rehashing bug in vfs_rename_dir */
  866. dn->d_time = jiffies;
  867. ceph_dentry(dn)->lease_shared_gen = 0;
  868. /* take overwritten dentry's readdir offset */
  869. ceph_dentry(req->r_old_dentry)->offset =
  870. ceph_dentry(dn)->offset;
  871. dn = req->r_old_dentry; /* use old_dentry */
  872. in = dn->d_inode;
  873. }
  874. /* null dentry? */
  875. if (!rinfo->head->is_target) {
  876. dout("fill_trace null dentry\n");
  877. if (dn->d_inode) {
  878. dout("d_delete %p\n", dn);
  879. d_delete(dn);
  880. } else {
  881. dout("d_instantiate %p NULL\n", dn);
  882. d_instantiate(dn, NULL);
  883. if (have_lease && d_unhashed(dn))
  884. d_rehash(dn);
  885. update_dentry_lease(dn, rinfo->dlease,
  886. session,
  887. req->r_request_started);
  888. }
  889. goto done;
  890. }
  891. /* attach proper inode */
  892. ininfo = rinfo->targeti.in;
  893. vino.ino = le64_to_cpu(ininfo->ino);
  894. vino.snap = le64_to_cpu(ininfo->snapid);
  895. if (!dn->d_inode) {
  896. in = ceph_get_inode(sb, vino);
  897. if (IS_ERR(in)) {
  898. pr_err("fill_trace bad get_inode "
  899. "%llx.%llx\n", vino.ino, vino.snap);
  900. err = PTR_ERR(in);
  901. d_delete(dn);
  902. goto done;
  903. }
  904. dn = splice_dentry(dn, in, &have_lease);
  905. if (IS_ERR(dn)) {
  906. err = PTR_ERR(dn);
  907. goto done;
  908. }
  909. req->r_dentry = dn; /* may have spliced */
  910. ceph_set_dentry_offset(dn);
  911. igrab(in);
  912. } else if (ceph_ino(in) == vino.ino &&
  913. ceph_snap(in) == vino.snap) {
  914. igrab(in);
  915. } else {
  916. dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
  917. dn, in, ceph_ino(in), ceph_snap(in),
  918. vino.ino, vino.snap);
  919. have_lease = false;
  920. in = NULL;
  921. }
  922. if (have_lease)
  923. update_dentry_lease(dn, rinfo->dlease, session,
  924. req->r_request_started);
  925. dout(" final dn %p\n", dn);
  926. i++;
  927. } else if (req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
  928. req->r_op == CEPH_MDS_OP_MKSNAP) {
  929. struct dentry *dn = req->r_dentry;
  930. /* fill out a snapdir LOOKUPSNAP dentry */
  931. BUG_ON(!dn);
  932. BUG_ON(!req->r_locked_dir);
  933. BUG_ON(ceph_snap(req->r_locked_dir) != CEPH_SNAPDIR);
  934. ininfo = rinfo->targeti.in;
  935. vino.ino = le64_to_cpu(ininfo->ino);
  936. vino.snap = le64_to_cpu(ininfo->snapid);
  937. in = ceph_get_inode(sb, vino);
  938. if (IS_ERR(in)) {
  939. pr_err("fill_inode get_inode badness %llx.%llx\n",
  940. vino.ino, vino.snap);
  941. err = PTR_ERR(in);
  942. d_delete(dn);
  943. goto done;
  944. }
  945. dout(" linking snapped dir %p to dn %p\n", in, dn);
  946. dn = splice_dentry(dn, in, NULL);
  947. if (IS_ERR(dn)) {
  948. err = PTR_ERR(dn);
  949. goto done;
  950. }
  951. ceph_set_dentry_offset(dn);
  952. req->r_dentry = dn; /* may have spliced */
  953. igrab(in);
  954. rinfo->head->is_dentry = 1; /* fool notrace handlers */
  955. }
  956. if (rinfo->head->is_target) {
  957. vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
  958. vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
  959. if (in == NULL || ceph_ino(in) != vino.ino ||
  960. ceph_snap(in) != vino.snap) {
  961. in = ceph_get_inode(sb, vino);
  962. if (IS_ERR(in)) {
  963. err = PTR_ERR(in);
  964. goto done;
  965. }
  966. }
  967. req->r_target_inode = in;
  968. err = fill_inode(in,
  969. &rinfo->targeti, NULL,
  970. session, req->r_request_started,
  971. (le32_to_cpu(rinfo->head->result) == 0) ?
  972. req->r_fmode : -1,
  973. &req->r_caps_reservation);
  974. if (err < 0) {
  975. pr_err("fill_inode badness %p %llx.%llx\n",
  976. in, ceph_vinop(in));
  977. goto done;
  978. }
  979. }
  980. done:
  981. dout("fill_trace done err=%d\n", err);
  982. return err;
  983. }
  984. /*
  985. * Prepopulate our cache with readdir results, leases, etc.
  986. */
  987. int ceph_readdir_prepopulate(struct ceph_mds_request *req,
  988. struct ceph_mds_session *session)
  989. {
  990. struct dentry *parent = req->r_dentry;
  991. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  992. struct qstr dname;
  993. struct dentry *dn;
  994. struct inode *in;
  995. int err = 0, i;
  996. struct inode *snapdir = NULL;
  997. struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
  998. u64 frag = le32_to_cpu(rhead->args.readdir.frag);
  999. struct ceph_dentry_info *di;
  1000. if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
  1001. snapdir = ceph_get_snapdir(parent->d_inode);
  1002. parent = d_find_alias(snapdir);
  1003. dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
  1004. rinfo->dir_nr, parent);
  1005. } else {
  1006. dout("readdir_prepopulate %d items under dn %p\n",
  1007. rinfo->dir_nr, parent);
  1008. if (rinfo->dir_dir)
  1009. ceph_fill_dirfrag(parent->d_inode, rinfo->dir_dir);
  1010. }
  1011. for (i = 0; i < rinfo->dir_nr; i++) {
  1012. struct ceph_vino vino;
  1013. dname.name = rinfo->dir_dname[i];
  1014. dname.len = rinfo->dir_dname_len[i];
  1015. dname.hash = full_name_hash(dname.name, dname.len);
  1016. vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
  1017. vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
  1018. retry_lookup:
  1019. dn = d_lookup(parent, &dname);
  1020. dout("d_lookup on parent=%p name=%.*s got %p\n",
  1021. parent, dname.len, dname.name, dn);
  1022. if (!dn) {
  1023. dn = d_alloc(parent, &dname);
  1024. dout("d_alloc %p '%.*s' = %p\n", parent,
  1025. dname.len, dname.name, dn);
  1026. if (dn == NULL) {
  1027. dout("d_alloc badness\n");
  1028. err = -ENOMEM;
  1029. goto out;
  1030. }
  1031. err = ceph_init_dentry(dn);
  1032. if (err < 0)
  1033. goto out;
  1034. } else if (dn->d_inode &&
  1035. (ceph_ino(dn->d_inode) != vino.ino ||
  1036. ceph_snap(dn->d_inode) != vino.snap)) {
  1037. dout(" dn %p points to wrong inode %p\n",
  1038. dn, dn->d_inode);
  1039. d_delete(dn);
  1040. dput(dn);
  1041. goto retry_lookup;
  1042. } else {
  1043. /* reorder parent's d_subdirs */
  1044. spin_lock(&dcache_lock);
  1045. spin_lock(&dn->d_lock);
  1046. list_move(&dn->d_u.d_child, &parent->d_subdirs);
  1047. spin_unlock(&dn->d_lock);
  1048. spin_unlock(&dcache_lock);
  1049. }
  1050. di = dn->d_fsdata;
  1051. di->offset = ceph_make_fpos(frag, i + req->r_readdir_offset);
  1052. /* inode */
  1053. if (dn->d_inode) {
  1054. in = dn->d_inode;
  1055. } else {
  1056. in = ceph_get_inode(parent->d_sb, vino);
  1057. if (in == NULL) {
  1058. dout("new_inode badness\n");
  1059. d_delete(dn);
  1060. dput(dn);
  1061. err = -ENOMEM;
  1062. goto out;
  1063. }
  1064. dn = splice_dentry(dn, in, NULL);
  1065. }
  1066. if (fill_inode(in, &rinfo->dir_in[i], NULL, session,
  1067. req->r_request_started, -1,
  1068. &req->r_caps_reservation) < 0) {
  1069. pr_err("fill_inode badness on %p\n", in);
  1070. dput(dn);
  1071. continue;
  1072. }
  1073. update_dentry_lease(dn, rinfo->dir_dlease[i],
  1074. req->r_session, req->r_request_started);
  1075. dput(dn);
  1076. }
  1077. req->r_did_prepopulate = true;
  1078. out:
  1079. if (snapdir) {
  1080. iput(snapdir);
  1081. dput(parent);
  1082. }
  1083. dout("readdir_prepopulate done\n");
  1084. return err;
  1085. }
  1086. int ceph_inode_set_size(struct inode *inode, loff_t size)
  1087. {
  1088. struct ceph_inode_info *ci = ceph_inode(inode);
  1089. int ret = 0;
  1090. spin_lock(&inode->i_lock);
  1091. dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
  1092. inode->i_size = size;
  1093. inode->i_blocks = (size + (1 << 9) - 1) >> 9;
  1094. /* tell the MDS if we are approaching max_size */
  1095. if ((size << 1) >= ci->i_max_size &&
  1096. (ci->i_reported_size << 1) < ci->i_max_size)
  1097. ret = 1;
  1098. spin_unlock(&inode->i_lock);
  1099. return ret;
  1100. }
  1101. /*
  1102. * Write back inode data in a worker thread. (This can't be done
  1103. * in the message handler context.)
  1104. */
  1105. void ceph_queue_writeback(struct inode *inode)
  1106. {
  1107. if (queue_work(ceph_inode_to_client(inode)->wb_wq,
  1108. &ceph_inode(inode)->i_wb_work)) {
  1109. dout("ceph_queue_writeback %p\n", inode);
  1110. igrab(inode);
  1111. } else {
  1112. dout("ceph_queue_writeback %p failed\n", inode);
  1113. }
  1114. }
  1115. static void ceph_writeback_work(struct work_struct *work)
  1116. {
  1117. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1118. i_wb_work);
  1119. struct inode *inode = &ci->vfs_inode;
  1120. dout("writeback %p\n", inode);
  1121. filemap_fdatawrite(&inode->i_data);
  1122. iput(inode);
  1123. }
  1124. /*
  1125. * queue an async invalidation
  1126. */
  1127. void ceph_queue_invalidate(struct inode *inode)
  1128. {
  1129. if (queue_work(ceph_inode_to_client(inode)->pg_inv_wq,
  1130. &ceph_inode(inode)->i_pg_inv_work)) {
  1131. dout("ceph_queue_invalidate %p\n", inode);
  1132. igrab(inode);
  1133. } else {
  1134. dout("ceph_queue_invalidate %p failed\n", inode);
  1135. }
  1136. }
  1137. /*
  1138. * Invalidate inode pages in a worker thread. (This can't be done
  1139. * in the message handler context.)
  1140. */
  1141. static void ceph_invalidate_work(struct work_struct *work)
  1142. {
  1143. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1144. i_pg_inv_work);
  1145. struct inode *inode = &ci->vfs_inode;
  1146. u32 orig_gen;
  1147. int check = 0;
  1148. spin_lock(&inode->i_lock);
  1149. dout("invalidate_pages %p gen %d revoking %d\n", inode,
  1150. ci->i_rdcache_gen, ci->i_rdcache_revoking);
  1151. if (ci->i_rdcache_gen == 0 ||
  1152. ci->i_rdcache_revoking != ci->i_rdcache_gen) {
  1153. BUG_ON(ci->i_rdcache_revoking > ci->i_rdcache_gen);
  1154. /* nevermind! */
  1155. ci->i_rdcache_revoking = 0;
  1156. spin_unlock(&inode->i_lock);
  1157. goto out;
  1158. }
  1159. orig_gen = ci->i_rdcache_gen;
  1160. spin_unlock(&inode->i_lock);
  1161. truncate_inode_pages(&inode->i_data, 0);
  1162. spin_lock(&inode->i_lock);
  1163. if (orig_gen == ci->i_rdcache_gen) {
  1164. dout("invalidate_pages %p gen %d successful\n", inode,
  1165. ci->i_rdcache_gen);
  1166. ci->i_rdcache_gen = 0;
  1167. ci->i_rdcache_revoking = 0;
  1168. check = 1;
  1169. } else {
  1170. dout("invalidate_pages %p gen %d raced, gen now %d\n",
  1171. inode, orig_gen, ci->i_rdcache_gen);
  1172. }
  1173. spin_unlock(&inode->i_lock);
  1174. if (check)
  1175. ceph_check_caps(ci, 0, NULL);
  1176. out:
  1177. iput(inode);
  1178. }
  1179. /*
  1180. * called by trunc_wq; take i_mutex ourselves
  1181. *
  1182. * We also truncate in a separate thread as well.
  1183. */
  1184. static void ceph_vmtruncate_work(struct work_struct *work)
  1185. {
  1186. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1187. i_vmtruncate_work);
  1188. struct inode *inode = &ci->vfs_inode;
  1189. dout("vmtruncate_work %p\n", inode);
  1190. mutex_lock(&inode->i_mutex);
  1191. __ceph_do_pending_vmtruncate(inode);
  1192. mutex_unlock(&inode->i_mutex);
  1193. iput(inode);
  1194. }
  1195. /*
  1196. * Queue an async vmtruncate. If we fail to queue work, we will handle
  1197. * the truncation the next time we call __ceph_do_pending_vmtruncate.
  1198. */
  1199. void ceph_queue_vmtruncate(struct inode *inode)
  1200. {
  1201. struct ceph_inode_info *ci = ceph_inode(inode);
  1202. if (queue_work(ceph_client(inode->i_sb)->trunc_wq,
  1203. &ci->i_vmtruncate_work)) {
  1204. dout("ceph_queue_vmtruncate %p\n", inode);
  1205. igrab(inode);
  1206. } else {
  1207. dout("ceph_queue_vmtruncate %p failed, pending=%d\n",
  1208. inode, ci->i_truncate_pending);
  1209. }
  1210. }
  1211. /*
  1212. * called with i_mutex held.
  1213. *
  1214. * Make sure any pending truncation is applied before doing anything
  1215. * that may depend on it.
  1216. */
  1217. void __ceph_do_pending_vmtruncate(struct inode *inode)
  1218. {
  1219. struct ceph_inode_info *ci = ceph_inode(inode);
  1220. u64 to;
  1221. int wrbuffer_refs, wake = 0;
  1222. retry:
  1223. spin_lock(&inode->i_lock);
  1224. if (ci->i_truncate_pending == 0) {
  1225. dout("__do_pending_vmtruncate %p none pending\n", inode);
  1226. spin_unlock(&inode->i_lock);
  1227. return;
  1228. }
  1229. /*
  1230. * make sure any dirty snapped pages are flushed before we
  1231. * possibly truncate them.. so write AND block!
  1232. */
  1233. if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
  1234. dout("__do_pending_vmtruncate %p flushing snaps first\n",
  1235. inode);
  1236. spin_unlock(&inode->i_lock);
  1237. filemap_write_and_wait_range(&inode->i_data, 0,
  1238. inode->i_sb->s_maxbytes);
  1239. goto retry;
  1240. }
  1241. to = ci->i_truncate_size;
  1242. wrbuffer_refs = ci->i_wrbuffer_ref;
  1243. dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
  1244. ci->i_truncate_pending, to);
  1245. spin_unlock(&inode->i_lock);
  1246. truncate_inode_pages(inode->i_mapping, to);
  1247. spin_lock(&inode->i_lock);
  1248. ci->i_truncate_pending--;
  1249. if (ci->i_truncate_pending == 0)
  1250. wake = 1;
  1251. spin_unlock(&inode->i_lock);
  1252. if (wrbuffer_refs == 0)
  1253. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  1254. if (wake)
  1255. wake_up(&ci->i_cap_wq);
  1256. }
  1257. /*
  1258. * symlinks
  1259. */
  1260. static void *ceph_sym_follow_link(struct dentry *dentry, struct nameidata *nd)
  1261. {
  1262. struct ceph_inode_info *ci = ceph_inode(dentry->d_inode);
  1263. nd_set_link(nd, ci->i_symlink);
  1264. return NULL;
  1265. }
  1266. static const struct inode_operations ceph_symlink_iops = {
  1267. .readlink = generic_readlink,
  1268. .follow_link = ceph_sym_follow_link,
  1269. };
  1270. /*
  1271. * setattr
  1272. */
  1273. int ceph_setattr(struct dentry *dentry, struct iattr *attr)
  1274. {
  1275. struct inode *inode = dentry->d_inode;
  1276. struct ceph_inode_info *ci = ceph_inode(inode);
  1277. struct inode *parent_inode = dentry->d_parent->d_inode;
  1278. const unsigned int ia_valid = attr->ia_valid;
  1279. struct ceph_mds_request *req;
  1280. struct ceph_mds_client *mdsc = &ceph_client(dentry->d_sb)->mdsc;
  1281. int issued;
  1282. int release = 0, dirtied = 0;
  1283. int mask = 0;
  1284. int err = 0;
  1285. if (ceph_snap(inode) != CEPH_NOSNAP)
  1286. return -EROFS;
  1287. __ceph_do_pending_vmtruncate(inode);
  1288. err = inode_change_ok(inode, attr);
  1289. if (err != 0)
  1290. return err;
  1291. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
  1292. USE_AUTH_MDS);
  1293. if (IS_ERR(req))
  1294. return PTR_ERR(req);
  1295. spin_lock(&inode->i_lock);
  1296. issued = __ceph_caps_issued(ci, NULL);
  1297. dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
  1298. if (ia_valid & ATTR_UID) {
  1299. dout("setattr %p uid %d -> %d\n", inode,
  1300. inode->i_uid, attr->ia_uid);
  1301. if (issued & CEPH_CAP_AUTH_EXCL) {
  1302. inode->i_uid = attr->ia_uid;
  1303. dirtied |= CEPH_CAP_AUTH_EXCL;
  1304. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1305. attr->ia_uid != inode->i_uid) {
  1306. req->r_args.setattr.uid = cpu_to_le32(attr->ia_uid);
  1307. mask |= CEPH_SETATTR_UID;
  1308. release |= CEPH_CAP_AUTH_SHARED;
  1309. }
  1310. }
  1311. if (ia_valid & ATTR_GID) {
  1312. dout("setattr %p gid %d -> %d\n", inode,
  1313. inode->i_gid, attr->ia_gid);
  1314. if (issued & CEPH_CAP_AUTH_EXCL) {
  1315. inode->i_gid = attr->ia_gid;
  1316. dirtied |= CEPH_CAP_AUTH_EXCL;
  1317. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1318. attr->ia_gid != inode->i_gid) {
  1319. req->r_args.setattr.gid = cpu_to_le32(attr->ia_gid);
  1320. mask |= CEPH_SETATTR_GID;
  1321. release |= CEPH_CAP_AUTH_SHARED;
  1322. }
  1323. }
  1324. if (ia_valid & ATTR_MODE) {
  1325. dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
  1326. attr->ia_mode);
  1327. if (issued & CEPH_CAP_AUTH_EXCL) {
  1328. inode->i_mode = attr->ia_mode;
  1329. dirtied |= CEPH_CAP_AUTH_EXCL;
  1330. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1331. attr->ia_mode != inode->i_mode) {
  1332. req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
  1333. mask |= CEPH_SETATTR_MODE;
  1334. release |= CEPH_CAP_AUTH_SHARED;
  1335. }
  1336. }
  1337. if (ia_valid & ATTR_ATIME) {
  1338. dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode,
  1339. inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
  1340. attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
  1341. if (issued & CEPH_CAP_FILE_EXCL) {
  1342. ci->i_time_warp_seq++;
  1343. inode->i_atime = attr->ia_atime;
  1344. dirtied |= CEPH_CAP_FILE_EXCL;
  1345. } else if ((issued & CEPH_CAP_FILE_WR) &&
  1346. timespec_compare(&inode->i_atime,
  1347. &attr->ia_atime) < 0) {
  1348. inode->i_atime = attr->ia_atime;
  1349. dirtied |= CEPH_CAP_FILE_WR;
  1350. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1351. !timespec_equal(&inode->i_atime, &attr->ia_atime)) {
  1352. ceph_encode_timespec(&req->r_args.setattr.atime,
  1353. &attr->ia_atime);
  1354. mask |= CEPH_SETATTR_ATIME;
  1355. release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD |
  1356. CEPH_CAP_FILE_WR;
  1357. }
  1358. }
  1359. if (ia_valid & ATTR_MTIME) {
  1360. dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode,
  1361. inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
  1362. attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
  1363. if (issued & CEPH_CAP_FILE_EXCL) {
  1364. ci->i_time_warp_seq++;
  1365. inode->i_mtime = attr->ia_mtime;
  1366. dirtied |= CEPH_CAP_FILE_EXCL;
  1367. } else if ((issued & CEPH_CAP_FILE_WR) &&
  1368. timespec_compare(&inode->i_mtime,
  1369. &attr->ia_mtime) < 0) {
  1370. inode->i_mtime = attr->ia_mtime;
  1371. dirtied |= CEPH_CAP_FILE_WR;
  1372. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1373. !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) {
  1374. ceph_encode_timespec(&req->r_args.setattr.mtime,
  1375. &attr->ia_mtime);
  1376. mask |= CEPH_SETATTR_MTIME;
  1377. release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
  1378. CEPH_CAP_FILE_WR;
  1379. }
  1380. }
  1381. if (ia_valid & ATTR_SIZE) {
  1382. dout("setattr %p size %lld -> %lld\n", inode,
  1383. inode->i_size, attr->ia_size);
  1384. if (attr->ia_size > inode->i_sb->s_maxbytes) {
  1385. err = -EINVAL;
  1386. goto out;
  1387. }
  1388. if ((issued & CEPH_CAP_FILE_EXCL) &&
  1389. attr->ia_size > inode->i_size) {
  1390. inode->i_size = attr->ia_size;
  1391. inode->i_blocks =
  1392. (attr->ia_size + (1 << 9) - 1) >> 9;
  1393. inode->i_ctime = attr->ia_ctime;
  1394. ci->i_reported_size = attr->ia_size;
  1395. dirtied |= CEPH_CAP_FILE_EXCL;
  1396. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1397. attr->ia_size != inode->i_size) {
  1398. req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
  1399. req->r_args.setattr.old_size =
  1400. cpu_to_le64(inode->i_size);
  1401. mask |= CEPH_SETATTR_SIZE;
  1402. release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
  1403. CEPH_CAP_FILE_WR;
  1404. }
  1405. }
  1406. /* these do nothing */
  1407. if (ia_valid & ATTR_CTIME) {
  1408. bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
  1409. ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
  1410. dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode,
  1411. inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
  1412. attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
  1413. only ? "ctime only" : "ignored");
  1414. inode->i_ctime = attr->ia_ctime;
  1415. if (only) {
  1416. /*
  1417. * if kernel wants to dirty ctime but nothing else,
  1418. * we need to choose a cap to dirty under, or do
  1419. * a almost-no-op setattr
  1420. */
  1421. if (issued & CEPH_CAP_AUTH_EXCL)
  1422. dirtied |= CEPH_CAP_AUTH_EXCL;
  1423. else if (issued & CEPH_CAP_FILE_EXCL)
  1424. dirtied |= CEPH_CAP_FILE_EXCL;
  1425. else if (issued & CEPH_CAP_XATTR_EXCL)
  1426. dirtied |= CEPH_CAP_XATTR_EXCL;
  1427. else
  1428. mask |= CEPH_SETATTR_CTIME;
  1429. }
  1430. }
  1431. if (ia_valid & ATTR_FILE)
  1432. dout("setattr %p ATTR_FILE ... hrm!\n", inode);
  1433. if (dirtied) {
  1434. __ceph_mark_dirty_caps(ci, dirtied);
  1435. inode->i_ctime = CURRENT_TIME;
  1436. }
  1437. release &= issued;
  1438. spin_unlock(&inode->i_lock);
  1439. if (mask) {
  1440. req->r_inode = igrab(inode);
  1441. req->r_inode_drop = release;
  1442. req->r_args.setattr.mask = cpu_to_le32(mask);
  1443. req->r_num_caps = 1;
  1444. err = ceph_mdsc_do_request(mdsc, parent_inode, req);
  1445. }
  1446. dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
  1447. ceph_cap_string(dirtied), mask);
  1448. ceph_mdsc_put_request(req);
  1449. __ceph_do_pending_vmtruncate(inode);
  1450. return err;
  1451. out:
  1452. spin_unlock(&inode->i_lock);
  1453. ceph_mdsc_put_request(req);
  1454. return err;
  1455. }
  1456. /*
  1457. * Verify that we have a lease on the given mask. If not,
  1458. * do a getattr against an mds.
  1459. */
  1460. int ceph_do_getattr(struct inode *inode, int mask)
  1461. {
  1462. struct ceph_client *client = ceph_sb_to_client(inode->i_sb);
  1463. struct ceph_mds_client *mdsc = &client->mdsc;
  1464. struct ceph_mds_request *req;
  1465. int err;
  1466. if (ceph_snap(inode) == CEPH_SNAPDIR) {
  1467. dout("do_getattr inode %p SNAPDIR\n", inode);
  1468. return 0;
  1469. }
  1470. dout("do_getattr inode %p mask %s\n", inode, ceph_cap_string(mask));
  1471. if (ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
  1472. return 0;
  1473. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
  1474. if (IS_ERR(req))
  1475. return PTR_ERR(req);
  1476. req->r_inode = igrab(inode);
  1477. req->r_num_caps = 1;
  1478. req->r_args.getattr.mask = cpu_to_le32(mask);
  1479. err = ceph_mdsc_do_request(mdsc, NULL, req);
  1480. ceph_mdsc_put_request(req);
  1481. dout("do_getattr result=%d\n", err);
  1482. return err;
  1483. }
  1484. /*
  1485. * Check inode permissions. We verify we have a valid value for
  1486. * the AUTH cap, then call the generic handler.
  1487. */
  1488. int ceph_permission(struct inode *inode, int mask)
  1489. {
  1490. int err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED);
  1491. if (!err)
  1492. err = generic_permission(inode, mask, NULL);
  1493. return err;
  1494. }
  1495. /*
  1496. * Get all attributes. Hopefully somedata we'll have a statlite()
  1497. * and can limit the fields we require to be accurate.
  1498. */
  1499. int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1500. struct kstat *stat)
  1501. {
  1502. struct inode *inode = dentry->d_inode;
  1503. struct ceph_inode_info *ci = ceph_inode(inode);
  1504. int err;
  1505. err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL);
  1506. if (!err) {
  1507. generic_fillattr(inode, stat);
  1508. stat->ino = inode->i_ino;
  1509. if (ceph_snap(inode) != CEPH_NOSNAP)
  1510. stat->dev = ceph_snap(inode);
  1511. else
  1512. stat->dev = 0;
  1513. if (S_ISDIR(inode->i_mode)) {
  1514. stat->size = ci->i_rbytes;
  1515. stat->blocks = 0;
  1516. stat->blksize = 65536;
  1517. }
  1518. }
  1519. return err;
  1520. }