inode.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/module.h>
  3. #include <linux/fs.h>
  4. #include <linux/slab.h>
  5. #include <linux/string.h>
  6. #include <linux/uaccess.h>
  7. #include <linux/kernel.h>
  8. #include <linux/writeback.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/xattr.h>
  11. #include <linux/posix_acl.h>
  12. #include <linux/random.h>
  13. #include <linux/sort.h>
  14. #include "super.h"
  15. #include "mds_client.h"
  16. #include "cache.h"
  17. #include <linux/ceph/decode.h>
  18. /*
  19. * Ceph inode operations
  20. *
  21. * Implement basic inode helpers (get, alloc) and inode ops (getattr,
  22. * setattr, etc.), xattr helpers, and helpers for assimilating
  23. * metadata returned by the MDS into our cache.
  24. *
  25. * Also define helpers for doing asynchronous writeback, invalidation,
  26. * and truncation for the benefit of those who can't afford to block
  27. * (typically because they are in the message handler path).
  28. */
  29. static const struct inode_operations ceph_symlink_iops;
  30. static void ceph_invalidate_work(struct work_struct *work);
  31. static void ceph_writeback_work(struct work_struct *work);
  32. static void ceph_vmtruncate_work(struct work_struct *work);
  33. /*
  34. * find or create an inode, given the ceph ino number
  35. */
  36. static int ceph_set_ino_cb(struct inode *inode, void *data)
  37. {
  38. ceph_inode(inode)->i_vino = *(struct ceph_vino *)data;
  39. inode->i_ino = ceph_vino_to_ino(*(struct ceph_vino *)data);
  40. return 0;
  41. }
  42. struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino)
  43. {
  44. struct inode *inode;
  45. ino_t t = ceph_vino_to_ino(vino);
  46. inode = iget5_locked(sb, t, ceph_ino_compare, ceph_set_ino_cb, &vino);
  47. if (!inode)
  48. return ERR_PTR(-ENOMEM);
  49. if (inode->i_state & I_NEW) {
  50. dout("get_inode created new inode %p %llx.%llx ino %llx\n",
  51. inode, ceph_vinop(inode), (u64)inode->i_ino);
  52. unlock_new_inode(inode);
  53. }
  54. dout("get_inode on %lu=%llx.%llx got %p\n", inode->i_ino, vino.ino,
  55. vino.snap, inode);
  56. return inode;
  57. }
  58. /*
  59. * get/constuct snapdir inode for a given directory
  60. */
  61. struct inode *ceph_get_snapdir(struct inode *parent)
  62. {
  63. struct ceph_vino vino = {
  64. .ino = ceph_ino(parent),
  65. .snap = CEPH_SNAPDIR,
  66. };
  67. struct inode *inode = ceph_get_inode(parent->i_sb, vino);
  68. struct ceph_inode_info *ci = ceph_inode(inode);
  69. BUG_ON(!S_ISDIR(parent->i_mode));
  70. if (IS_ERR(inode))
  71. return inode;
  72. inode->i_mode = parent->i_mode;
  73. inode->i_uid = parent->i_uid;
  74. inode->i_gid = parent->i_gid;
  75. inode->i_op = &ceph_snapdir_iops;
  76. inode->i_fop = &ceph_snapdir_fops;
  77. ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
  78. ci->i_rbytes = 0;
  79. return inode;
  80. }
  81. const struct inode_operations ceph_file_iops = {
  82. .permission = ceph_permission,
  83. .setattr = ceph_setattr,
  84. .getattr = ceph_getattr,
  85. .listxattr = ceph_listxattr,
  86. .get_acl = ceph_get_acl,
  87. .set_acl = ceph_set_acl,
  88. };
  89. /*
  90. * We use a 'frag tree' to keep track of the MDS's directory fragments
  91. * for a given inode (usually there is just a single fragment). We
  92. * need to know when a child frag is delegated to a new MDS, or when
  93. * it is flagged as replicated, so we can direct our requests
  94. * accordingly.
  95. */
  96. /*
  97. * find/create a frag in the tree
  98. */
  99. static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
  100. u32 f)
  101. {
  102. struct rb_node **p;
  103. struct rb_node *parent = NULL;
  104. struct ceph_inode_frag *frag;
  105. int c;
  106. p = &ci->i_fragtree.rb_node;
  107. while (*p) {
  108. parent = *p;
  109. frag = rb_entry(parent, struct ceph_inode_frag, node);
  110. c = ceph_frag_compare(f, frag->frag);
  111. if (c < 0)
  112. p = &(*p)->rb_left;
  113. else if (c > 0)
  114. p = &(*p)->rb_right;
  115. else
  116. return frag;
  117. }
  118. frag = kmalloc(sizeof(*frag), GFP_NOFS);
  119. if (!frag)
  120. return ERR_PTR(-ENOMEM);
  121. frag->frag = f;
  122. frag->split_by = 0;
  123. frag->mds = -1;
  124. frag->ndist = 0;
  125. rb_link_node(&frag->node, parent, p);
  126. rb_insert_color(&frag->node, &ci->i_fragtree);
  127. dout("get_or_create_frag added %llx.%llx frag %x\n",
  128. ceph_vinop(&ci->vfs_inode), f);
  129. return frag;
  130. }
  131. /*
  132. * find a specific frag @f
  133. */
  134. struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
  135. {
  136. struct rb_node *n = ci->i_fragtree.rb_node;
  137. while (n) {
  138. struct ceph_inode_frag *frag =
  139. rb_entry(n, struct ceph_inode_frag, node);
  140. int c = ceph_frag_compare(f, frag->frag);
  141. if (c < 0)
  142. n = n->rb_left;
  143. else if (c > 0)
  144. n = n->rb_right;
  145. else
  146. return frag;
  147. }
  148. return NULL;
  149. }
  150. /*
  151. * Choose frag containing the given value @v. If @pfrag is
  152. * specified, copy the frag delegation info to the caller if
  153. * it is present.
  154. */
  155. static u32 __ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
  156. struct ceph_inode_frag *pfrag, int *found)
  157. {
  158. u32 t = ceph_frag_make(0, 0);
  159. struct ceph_inode_frag *frag;
  160. unsigned nway, i;
  161. u32 n;
  162. if (found)
  163. *found = 0;
  164. while (1) {
  165. WARN_ON(!ceph_frag_contains_value(t, v));
  166. frag = __ceph_find_frag(ci, t);
  167. if (!frag)
  168. break; /* t is a leaf */
  169. if (frag->split_by == 0) {
  170. if (pfrag)
  171. memcpy(pfrag, frag, sizeof(*pfrag));
  172. if (found)
  173. *found = 1;
  174. break;
  175. }
  176. /* choose child */
  177. nway = 1 << frag->split_by;
  178. dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t,
  179. frag->split_by, nway);
  180. for (i = 0; i < nway; i++) {
  181. n = ceph_frag_make_child(t, frag->split_by, i);
  182. if (ceph_frag_contains_value(n, v)) {
  183. t = n;
  184. break;
  185. }
  186. }
  187. BUG_ON(i == nway);
  188. }
  189. dout("choose_frag(%x) = %x\n", v, t);
  190. return t;
  191. }
  192. u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
  193. struct ceph_inode_frag *pfrag, int *found)
  194. {
  195. u32 ret;
  196. mutex_lock(&ci->i_fragtree_mutex);
  197. ret = __ceph_choose_frag(ci, v, pfrag, found);
  198. mutex_unlock(&ci->i_fragtree_mutex);
  199. return ret;
  200. }
  201. /*
  202. * Process dirfrag (delegation) info from the mds. Include leaf
  203. * fragment in tree ONLY if ndist > 0. Otherwise, only
  204. * branches/splits are included in i_fragtree)
  205. */
  206. static int ceph_fill_dirfrag(struct inode *inode,
  207. struct ceph_mds_reply_dirfrag *dirinfo)
  208. {
  209. struct ceph_inode_info *ci = ceph_inode(inode);
  210. struct ceph_inode_frag *frag;
  211. u32 id = le32_to_cpu(dirinfo->frag);
  212. int mds = le32_to_cpu(dirinfo->auth);
  213. int ndist = le32_to_cpu(dirinfo->ndist);
  214. int diri_auth = -1;
  215. int i;
  216. int err = 0;
  217. spin_lock(&ci->i_ceph_lock);
  218. if (ci->i_auth_cap)
  219. diri_auth = ci->i_auth_cap->mds;
  220. spin_unlock(&ci->i_ceph_lock);
  221. if (mds == -1) /* CDIR_AUTH_PARENT */
  222. mds = diri_auth;
  223. mutex_lock(&ci->i_fragtree_mutex);
  224. if (ndist == 0 && mds == diri_auth) {
  225. /* no delegation info needed. */
  226. frag = __ceph_find_frag(ci, id);
  227. if (!frag)
  228. goto out;
  229. if (frag->split_by == 0) {
  230. /* tree leaf, remove */
  231. dout("fill_dirfrag removed %llx.%llx frag %x"
  232. " (no ref)\n", ceph_vinop(inode), id);
  233. rb_erase(&frag->node, &ci->i_fragtree);
  234. kfree(frag);
  235. } else {
  236. /* tree branch, keep and clear */
  237. dout("fill_dirfrag cleared %llx.%llx frag %x"
  238. " referral\n", ceph_vinop(inode), id);
  239. frag->mds = -1;
  240. frag->ndist = 0;
  241. }
  242. goto out;
  243. }
  244. /* find/add this frag to store mds delegation info */
  245. frag = __get_or_create_frag(ci, id);
  246. if (IS_ERR(frag)) {
  247. /* this is not the end of the world; we can continue
  248. with bad/inaccurate delegation info */
  249. pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n",
  250. ceph_vinop(inode), le32_to_cpu(dirinfo->frag));
  251. err = -ENOMEM;
  252. goto out;
  253. }
  254. frag->mds = mds;
  255. frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
  256. for (i = 0; i < frag->ndist; i++)
  257. frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
  258. dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n",
  259. ceph_vinop(inode), frag->frag, frag->ndist);
  260. out:
  261. mutex_unlock(&ci->i_fragtree_mutex);
  262. return err;
  263. }
  264. static int frag_tree_split_cmp(const void *l, const void *r)
  265. {
  266. struct ceph_frag_tree_split *ls = (struct ceph_frag_tree_split*)l;
  267. struct ceph_frag_tree_split *rs = (struct ceph_frag_tree_split*)r;
  268. return ceph_frag_compare(le32_to_cpu(ls->frag),
  269. le32_to_cpu(rs->frag));
  270. }
  271. static bool is_frag_child(u32 f, struct ceph_inode_frag *frag)
  272. {
  273. if (!frag)
  274. return f == ceph_frag_make(0, 0);
  275. if (ceph_frag_bits(f) != ceph_frag_bits(frag->frag) + frag->split_by)
  276. return false;
  277. return ceph_frag_contains_value(frag->frag, ceph_frag_value(f));
  278. }
  279. static int ceph_fill_fragtree(struct inode *inode,
  280. struct ceph_frag_tree_head *fragtree,
  281. struct ceph_mds_reply_dirfrag *dirinfo)
  282. {
  283. struct ceph_inode_info *ci = ceph_inode(inode);
  284. struct ceph_inode_frag *frag, *prev_frag = NULL;
  285. struct rb_node *rb_node;
  286. unsigned i, split_by, nsplits;
  287. u32 id;
  288. bool update = false;
  289. mutex_lock(&ci->i_fragtree_mutex);
  290. nsplits = le32_to_cpu(fragtree->nsplits);
  291. if (nsplits != ci->i_fragtree_nsplits) {
  292. update = true;
  293. } else if (nsplits) {
  294. i = prandom_u32() % nsplits;
  295. id = le32_to_cpu(fragtree->splits[i].frag);
  296. if (!__ceph_find_frag(ci, id))
  297. update = true;
  298. } else if (!RB_EMPTY_ROOT(&ci->i_fragtree)) {
  299. rb_node = rb_first(&ci->i_fragtree);
  300. frag = rb_entry(rb_node, struct ceph_inode_frag, node);
  301. if (frag->frag != ceph_frag_make(0, 0) || rb_next(rb_node))
  302. update = true;
  303. }
  304. if (!update && dirinfo) {
  305. id = le32_to_cpu(dirinfo->frag);
  306. if (id != __ceph_choose_frag(ci, id, NULL, NULL))
  307. update = true;
  308. }
  309. if (!update)
  310. goto out_unlock;
  311. if (nsplits > 1) {
  312. sort(fragtree->splits, nsplits, sizeof(fragtree->splits[0]),
  313. frag_tree_split_cmp, NULL);
  314. }
  315. dout("fill_fragtree %llx.%llx\n", ceph_vinop(inode));
  316. rb_node = rb_first(&ci->i_fragtree);
  317. for (i = 0; i < nsplits; i++) {
  318. id = le32_to_cpu(fragtree->splits[i].frag);
  319. split_by = le32_to_cpu(fragtree->splits[i].by);
  320. if (split_by == 0 || ceph_frag_bits(id) + split_by > 24) {
  321. pr_err("fill_fragtree %llx.%llx invalid split %d/%u, "
  322. "frag %x split by %d\n", ceph_vinop(inode),
  323. i, nsplits, id, split_by);
  324. continue;
  325. }
  326. frag = NULL;
  327. while (rb_node) {
  328. frag = rb_entry(rb_node, struct ceph_inode_frag, node);
  329. if (ceph_frag_compare(frag->frag, id) >= 0) {
  330. if (frag->frag != id)
  331. frag = NULL;
  332. else
  333. rb_node = rb_next(rb_node);
  334. break;
  335. }
  336. rb_node = rb_next(rb_node);
  337. /* delete stale split/leaf node */
  338. if (frag->split_by > 0 ||
  339. !is_frag_child(frag->frag, prev_frag)) {
  340. rb_erase(&frag->node, &ci->i_fragtree);
  341. if (frag->split_by > 0)
  342. ci->i_fragtree_nsplits--;
  343. kfree(frag);
  344. }
  345. frag = NULL;
  346. }
  347. if (!frag) {
  348. frag = __get_or_create_frag(ci, id);
  349. if (IS_ERR(frag))
  350. continue;
  351. }
  352. if (frag->split_by == 0)
  353. ci->i_fragtree_nsplits++;
  354. frag->split_by = split_by;
  355. dout(" frag %x split by %d\n", frag->frag, frag->split_by);
  356. prev_frag = frag;
  357. }
  358. while (rb_node) {
  359. frag = rb_entry(rb_node, struct ceph_inode_frag, node);
  360. rb_node = rb_next(rb_node);
  361. /* delete stale split/leaf node */
  362. if (frag->split_by > 0 ||
  363. !is_frag_child(frag->frag, prev_frag)) {
  364. rb_erase(&frag->node, &ci->i_fragtree);
  365. if (frag->split_by > 0)
  366. ci->i_fragtree_nsplits--;
  367. kfree(frag);
  368. }
  369. }
  370. out_unlock:
  371. mutex_unlock(&ci->i_fragtree_mutex);
  372. return 0;
  373. }
  374. /*
  375. * initialize a newly allocated inode.
  376. */
  377. struct inode *ceph_alloc_inode(struct super_block *sb)
  378. {
  379. struct ceph_inode_info *ci;
  380. int i;
  381. ci = kmem_cache_alloc(ceph_inode_cachep, GFP_NOFS);
  382. if (!ci)
  383. return NULL;
  384. dout("alloc_inode %p\n", &ci->vfs_inode);
  385. spin_lock_init(&ci->i_ceph_lock);
  386. ci->i_version = 0;
  387. ci->i_inline_version = 0;
  388. ci->i_time_warp_seq = 0;
  389. ci->i_ceph_flags = 0;
  390. atomic64_set(&ci->i_ordered_count, 1);
  391. atomic64_set(&ci->i_release_count, 1);
  392. atomic64_set(&ci->i_complete_seq[0], 0);
  393. atomic64_set(&ci->i_complete_seq[1], 0);
  394. ci->i_symlink = NULL;
  395. memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout));
  396. RCU_INIT_POINTER(ci->i_layout.pool_ns, NULL);
  397. ci->i_fragtree = RB_ROOT;
  398. mutex_init(&ci->i_fragtree_mutex);
  399. ci->i_xattrs.blob = NULL;
  400. ci->i_xattrs.prealloc_blob = NULL;
  401. ci->i_xattrs.dirty = false;
  402. ci->i_xattrs.index = RB_ROOT;
  403. ci->i_xattrs.count = 0;
  404. ci->i_xattrs.names_size = 0;
  405. ci->i_xattrs.vals_size = 0;
  406. ci->i_xattrs.version = 0;
  407. ci->i_xattrs.index_version = 0;
  408. ci->i_caps = RB_ROOT;
  409. ci->i_auth_cap = NULL;
  410. ci->i_dirty_caps = 0;
  411. ci->i_flushing_caps = 0;
  412. INIT_LIST_HEAD(&ci->i_dirty_item);
  413. INIT_LIST_HEAD(&ci->i_flushing_item);
  414. ci->i_prealloc_cap_flush = NULL;
  415. INIT_LIST_HEAD(&ci->i_cap_flush_list);
  416. init_waitqueue_head(&ci->i_cap_wq);
  417. ci->i_hold_caps_min = 0;
  418. ci->i_hold_caps_max = 0;
  419. INIT_LIST_HEAD(&ci->i_cap_delay_list);
  420. INIT_LIST_HEAD(&ci->i_cap_snaps);
  421. ci->i_head_snapc = NULL;
  422. ci->i_snap_caps = 0;
  423. for (i = 0; i < CEPH_FILE_MODE_BITS; i++)
  424. ci->i_nr_by_mode[i] = 0;
  425. mutex_init(&ci->i_truncate_mutex);
  426. ci->i_truncate_seq = 0;
  427. ci->i_truncate_size = 0;
  428. ci->i_truncate_pending = 0;
  429. ci->i_max_size = 0;
  430. ci->i_reported_size = 0;
  431. ci->i_wanted_max_size = 0;
  432. ci->i_requested_max_size = 0;
  433. ci->i_pin_ref = 0;
  434. ci->i_rd_ref = 0;
  435. ci->i_rdcache_ref = 0;
  436. ci->i_wr_ref = 0;
  437. ci->i_wb_ref = 0;
  438. ci->i_wrbuffer_ref = 0;
  439. ci->i_wrbuffer_ref_head = 0;
  440. ci->i_shared_gen = 0;
  441. ci->i_rdcache_gen = 0;
  442. ci->i_rdcache_revoking = 0;
  443. INIT_LIST_HEAD(&ci->i_unsafe_dirops);
  444. INIT_LIST_HEAD(&ci->i_unsafe_iops);
  445. spin_lock_init(&ci->i_unsafe_lock);
  446. ci->i_snap_realm = NULL;
  447. INIT_LIST_HEAD(&ci->i_snap_realm_item);
  448. INIT_LIST_HEAD(&ci->i_snap_flush_item);
  449. INIT_WORK(&ci->i_wb_work, ceph_writeback_work);
  450. INIT_WORK(&ci->i_pg_inv_work, ceph_invalidate_work);
  451. INIT_WORK(&ci->i_vmtruncate_work, ceph_vmtruncate_work);
  452. ceph_fscache_inode_init(ci);
  453. return &ci->vfs_inode;
  454. }
  455. static void ceph_i_callback(struct rcu_head *head)
  456. {
  457. struct inode *inode = container_of(head, struct inode, i_rcu);
  458. struct ceph_inode_info *ci = ceph_inode(inode);
  459. kmem_cache_free(ceph_inode_cachep, ci);
  460. }
  461. void ceph_destroy_inode(struct inode *inode)
  462. {
  463. struct ceph_inode_info *ci = ceph_inode(inode);
  464. struct ceph_inode_frag *frag;
  465. struct rb_node *n;
  466. dout("destroy_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
  467. ceph_fscache_unregister_inode_cookie(ci);
  468. ceph_queue_caps_release(inode);
  469. /*
  470. * we may still have a snap_realm reference if there are stray
  471. * caps in i_snap_caps.
  472. */
  473. if (ci->i_snap_realm) {
  474. struct ceph_mds_client *mdsc =
  475. ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
  476. struct ceph_snap_realm *realm = ci->i_snap_realm;
  477. dout(" dropping residual ref to snap realm %p\n", realm);
  478. spin_lock(&realm->inodes_with_caps_lock);
  479. list_del_init(&ci->i_snap_realm_item);
  480. spin_unlock(&realm->inodes_with_caps_lock);
  481. ceph_put_snap_realm(mdsc, realm);
  482. }
  483. kfree(ci->i_symlink);
  484. while ((n = rb_first(&ci->i_fragtree)) != NULL) {
  485. frag = rb_entry(n, struct ceph_inode_frag, node);
  486. rb_erase(n, &ci->i_fragtree);
  487. kfree(frag);
  488. }
  489. ci->i_fragtree_nsplits = 0;
  490. __ceph_destroy_xattrs(ci);
  491. if (ci->i_xattrs.blob)
  492. ceph_buffer_put(ci->i_xattrs.blob);
  493. if (ci->i_xattrs.prealloc_blob)
  494. ceph_buffer_put(ci->i_xattrs.prealloc_blob);
  495. ceph_put_string(rcu_dereference_raw(ci->i_layout.pool_ns));
  496. call_rcu(&inode->i_rcu, ceph_i_callback);
  497. }
  498. int ceph_drop_inode(struct inode *inode)
  499. {
  500. /*
  501. * Positve dentry and corresponding inode are always accompanied
  502. * in MDS reply. So no need to keep inode in the cache after
  503. * dropping all its aliases.
  504. */
  505. return 1;
  506. }
  507. static inline blkcnt_t calc_inode_blocks(u64 size)
  508. {
  509. return (size + (1<<9) - 1) >> 9;
  510. }
  511. /*
  512. * Helpers to fill in size, ctime, mtime, and atime. We have to be
  513. * careful because either the client or MDS may have more up to date
  514. * info, depending on which capabilities are held, and whether
  515. * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
  516. * and size are monotonically increasing, except when utimes() or
  517. * truncate() increments the corresponding _seq values.)
  518. */
  519. int ceph_fill_file_size(struct inode *inode, int issued,
  520. u32 truncate_seq, u64 truncate_size, u64 size)
  521. {
  522. struct ceph_inode_info *ci = ceph_inode(inode);
  523. int queue_trunc = 0;
  524. if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
  525. (truncate_seq == ci->i_truncate_seq && size > inode->i_size)) {
  526. dout("size %lld -> %llu\n", inode->i_size, size);
  527. if (size > 0 && S_ISDIR(inode->i_mode)) {
  528. pr_err("fill_file_size non-zero size for directory\n");
  529. size = 0;
  530. }
  531. i_size_write(inode, size);
  532. inode->i_blocks = calc_inode_blocks(size);
  533. ci->i_reported_size = size;
  534. if (truncate_seq != ci->i_truncate_seq) {
  535. dout("truncate_seq %u -> %u\n",
  536. ci->i_truncate_seq, truncate_seq);
  537. ci->i_truncate_seq = truncate_seq;
  538. /* the MDS should have revoked these caps */
  539. WARN_ON_ONCE(issued & (CEPH_CAP_FILE_EXCL |
  540. CEPH_CAP_FILE_RD |
  541. CEPH_CAP_FILE_WR |
  542. CEPH_CAP_FILE_LAZYIO));
  543. /*
  544. * If we hold relevant caps, or in the case where we're
  545. * not the only client referencing this file and we
  546. * don't hold those caps, then we need to check whether
  547. * the file is either opened or mmaped
  548. */
  549. if ((issued & (CEPH_CAP_FILE_CACHE|
  550. CEPH_CAP_FILE_BUFFER)) ||
  551. mapping_mapped(inode->i_mapping) ||
  552. __ceph_caps_file_wanted(ci)) {
  553. ci->i_truncate_pending++;
  554. queue_trunc = 1;
  555. }
  556. }
  557. }
  558. if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
  559. ci->i_truncate_size != truncate_size) {
  560. dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
  561. truncate_size);
  562. ci->i_truncate_size = truncate_size;
  563. }
  564. if (queue_trunc)
  565. ceph_fscache_invalidate(inode);
  566. return queue_trunc;
  567. }
  568. void ceph_fill_file_time(struct inode *inode, int issued,
  569. u64 time_warp_seq, struct timespec *ctime,
  570. struct timespec *mtime, struct timespec *atime)
  571. {
  572. struct ceph_inode_info *ci = ceph_inode(inode);
  573. int warn = 0;
  574. if (issued & (CEPH_CAP_FILE_EXCL|
  575. CEPH_CAP_FILE_WR|
  576. CEPH_CAP_FILE_BUFFER|
  577. CEPH_CAP_AUTH_EXCL|
  578. CEPH_CAP_XATTR_EXCL)) {
  579. if (timespec_compare(ctime, &inode->i_ctime) > 0) {
  580. dout("ctime %ld.%09ld -> %ld.%09ld inc w/ cap\n",
  581. inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
  582. ctime->tv_sec, ctime->tv_nsec);
  583. inode->i_ctime = *ctime;
  584. }
  585. if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
  586. /* the MDS did a utimes() */
  587. dout("mtime %ld.%09ld -> %ld.%09ld "
  588. "tw %d -> %d\n",
  589. inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
  590. mtime->tv_sec, mtime->tv_nsec,
  591. ci->i_time_warp_seq, (int)time_warp_seq);
  592. inode->i_mtime = *mtime;
  593. inode->i_atime = *atime;
  594. ci->i_time_warp_seq = time_warp_seq;
  595. } else if (time_warp_seq == ci->i_time_warp_seq) {
  596. /* nobody did utimes(); take the max */
  597. if (timespec_compare(mtime, &inode->i_mtime) > 0) {
  598. dout("mtime %ld.%09ld -> %ld.%09ld inc\n",
  599. inode->i_mtime.tv_sec,
  600. inode->i_mtime.tv_nsec,
  601. mtime->tv_sec, mtime->tv_nsec);
  602. inode->i_mtime = *mtime;
  603. }
  604. if (timespec_compare(atime, &inode->i_atime) > 0) {
  605. dout("atime %ld.%09ld -> %ld.%09ld inc\n",
  606. inode->i_atime.tv_sec,
  607. inode->i_atime.tv_nsec,
  608. atime->tv_sec, atime->tv_nsec);
  609. inode->i_atime = *atime;
  610. }
  611. } else if (issued & CEPH_CAP_FILE_EXCL) {
  612. /* we did a utimes(); ignore mds values */
  613. } else {
  614. warn = 1;
  615. }
  616. } else {
  617. /* we have no write|excl caps; whatever the MDS says is true */
  618. if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
  619. inode->i_ctime = *ctime;
  620. inode->i_mtime = *mtime;
  621. inode->i_atime = *atime;
  622. ci->i_time_warp_seq = time_warp_seq;
  623. } else {
  624. warn = 1;
  625. }
  626. }
  627. if (warn) /* time_warp_seq shouldn't go backwards */
  628. dout("%p mds time_warp_seq %llu < %u\n",
  629. inode, time_warp_seq, ci->i_time_warp_seq);
  630. }
  631. /*
  632. * Populate an inode based on info from mds. May be called on new or
  633. * existing inodes.
  634. */
  635. static int fill_inode(struct inode *inode, struct page *locked_page,
  636. struct ceph_mds_reply_info_in *iinfo,
  637. struct ceph_mds_reply_dirfrag *dirinfo,
  638. struct ceph_mds_session *session,
  639. unsigned long ttl_from, int cap_fmode,
  640. struct ceph_cap_reservation *caps_reservation)
  641. {
  642. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  643. struct ceph_mds_reply_inode *info = iinfo->in;
  644. struct ceph_inode_info *ci = ceph_inode(inode);
  645. int issued = 0, implemented, new_issued;
  646. struct timespec mtime, atime, ctime;
  647. struct ceph_buffer *xattr_blob = NULL;
  648. struct ceph_string *pool_ns = NULL;
  649. struct ceph_cap *new_cap = NULL;
  650. int err = 0;
  651. bool wake = false;
  652. bool queue_trunc = false;
  653. bool new_version = false;
  654. bool fill_inline = false;
  655. dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
  656. inode, ceph_vinop(inode), le64_to_cpu(info->version),
  657. ci->i_version);
  658. /* prealloc new cap struct */
  659. if (info->cap.caps && ceph_snap(inode) == CEPH_NOSNAP)
  660. new_cap = ceph_get_cap(mdsc, caps_reservation);
  661. /*
  662. * prealloc xattr data, if it looks like we'll need it. only
  663. * if len > 4 (meaning there are actually xattrs; the first 4
  664. * bytes are the xattr count).
  665. */
  666. if (iinfo->xattr_len > 4) {
  667. xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
  668. if (!xattr_blob)
  669. pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
  670. iinfo->xattr_len);
  671. }
  672. if (iinfo->pool_ns_len > 0)
  673. pool_ns = ceph_find_or_create_string(iinfo->pool_ns_data,
  674. iinfo->pool_ns_len);
  675. spin_lock(&ci->i_ceph_lock);
  676. /*
  677. * provided version will be odd if inode value is projected,
  678. * even if stable. skip the update if we have newer stable
  679. * info (ours>=theirs, e.g. due to racing mds replies), unless
  680. * we are getting projected (unstable) info (in which case the
  681. * version is odd, and we want ours>theirs).
  682. * us them
  683. * 2 2 skip
  684. * 3 2 skip
  685. * 3 3 update
  686. */
  687. if (ci->i_version == 0 ||
  688. ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
  689. le64_to_cpu(info->version) > (ci->i_version & ~1)))
  690. new_version = true;
  691. issued = __ceph_caps_issued(ci, &implemented);
  692. issued |= implemented | __ceph_caps_dirty(ci);
  693. new_issued = ~issued & le32_to_cpu(info->cap.caps);
  694. /* update inode */
  695. ci->i_version = le64_to_cpu(info->version);
  696. inode->i_version++;
  697. inode->i_rdev = le32_to_cpu(info->rdev);
  698. inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
  699. if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) &&
  700. (issued & CEPH_CAP_AUTH_EXCL) == 0) {
  701. inode->i_mode = le32_to_cpu(info->mode);
  702. inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid));
  703. inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid));
  704. dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
  705. from_kuid(&init_user_ns, inode->i_uid),
  706. from_kgid(&init_user_ns, inode->i_gid));
  707. }
  708. if ((new_version || (new_issued & CEPH_CAP_LINK_SHARED)) &&
  709. (issued & CEPH_CAP_LINK_EXCL) == 0)
  710. set_nlink(inode, le32_to_cpu(info->nlink));
  711. if (new_version || (new_issued & CEPH_CAP_ANY_RD)) {
  712. /* be careful with mtime, atime, size */
  713. ceph_decode_timespec(&atime, &info->atime);
  714. ceph_decode_timespec(&mtime, &info->mtime);
  715. ceph_decode_timespec(&ctime, &info->ctime);
  716. ceph_fill_file_time(inode, issued,
  717. le32_to_cpu(info->time_warp_seq),
  718. &ctime, &mtime, &atime);
  719. }
  720. if (new_version ||
  721. (new_issued & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR))) {
  722. s64 old_pool = ci->i_layout.pool_id;
  723. struct ceph_string *old_ns;
  724. ceph_file_layout_from_legacy(&ci->i_layout, &info->layout);
  725. old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
  726. lockdep_is_held(&ci->i_ceph_lock));
  727. rcu_assign_pointer(ci->i_layout.pool_ns, pool_ns);
  728. if (ci->i_layout.pool_id != old_pool || pool_ns != old_ns)
  729. ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
  730. pool_ns = old_ns;
  731. queue_trunc = ceph_fill_file_size(inode, issued,
  732. le32_to_cpu(info->truncate_seq),
  733. le64_to_cpu(info->truncate_size),
  734. le64_to_cpu(info->size));
  735. /* only update max_size on auth cap */
  736. if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
  737. ci->i_max_size != le64_to_cpu(info->max_size)) {
  738. dout("max_size %lld -> %llu\n", ci->i_max_size,
  739. le64_to_cpu(info->max_size));
  740. ci->i_max_size = le64_to_cpu(info->max_size);
  741. }
  742. }
  743. /* xattrs */
  744. /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
  745. if ((ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) &&
  746. le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
  747. if (ci->i_xattrs.blob)
  748. ceph_buffer_put(ci->i_xattrs.blob);
  749. ci->i_xattrs.blob = xattr_blob;
  750. if (xattr_blob)
  751. memcpy(ci->i_xattrs.blob->vec.iov_base,
  752. iinfo->xattr_data, iinfo->xattr_len);
  753. ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
  754. ceph_forget_all_cached_acls(inode);
  755. xattr_blob = NULL;
  756. }
  757. inode->i_mapping->a_ops = &ceph_aops;
  758. switch (inode->i_mode & S_IFMT) {
  759. case S_IFIFO:
  760. case S_IFBLK:
  761. case S_IFCHR:
  762. case S_IFSOCK:
  763. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  764. inode->i_op = &ceph_file_iops;
  765. break;
  766. case S_IFREG:
  767. inode->i_op = &ceph_file_iops;
  768. inode->i_fop = &ceph_file_fops;
  769. break;
  770. case S_IFLNK:
  771. inode->i_op = &ceph_symlink_iops;
  772. if (!ci->i_symlink) {
  773. u32 symlen = iinfo->symlink_len;
  774. char *sym;
  775. spin_unlock(&ci->i_ceph_lock);
  776. if (symlen != i_size_read(inode)) {
  777. pr_err("fill_inode %llx.%llx BAD symlink "
  778. "size %lld\n", ceph_vinop(inode),
  779. i_size_read(inode));
  780. i_size_write(inode, symlen);
  781. inode->i_blocks = calc_inode_blocks(symlen);
  782. }
  783. err = -ENOMEM;
  784. sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS);
  785. if (!sym)
  786. goto out;
  787. spin_lock(&ci->i_ceph_lock);
  788. if (!ci->i_symlink)
  789. ci->i_symlink = sym;
  790. else
  791. kfree(sym); /* lost a race */
  792. }
  793. inode->i_link = ci->i_symlink;
  794. break;
  795. case S_IFDIR:
  796. inode->i_op = &ceph_dir_iops;
  797. inode->i_fop = &ceph_dir_fops;
  798. ci->i_dir_layout = iinfo->dir_layout;
  799. ci->i_files = le64_to_cpu(info->files);
  800. ci->i_subdirs = le64_to_cpu(info->subdirs);
  801. ci->i_rbytes = le64_to_cpu(info->rbytes);
  802. ci->i_rfiles = le64_to_cpu(info->rfiles);
  803. ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
  804. ceph_decode_timespec(&ci->i_rctime, &info->rctime);
  805. break;
  806. default:
  807. pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
  808. ceph_vinop(inode), inode->i_mode);
  809. }
  810. /* were we issued a capability? */
  811. if (info->cap.caps) {
  812. if (ceph_snap(inode) == CEPH_NOSNAP) {
  813. unsigned caps = le32_to_cpu(info->cap.caps);
  814. ceph_add_cap(inode, session,
  815. le64_to_cpu(info->cap.cap_id),
  816. cap_fmode, caps,
  817. le32_to_cpu(info->cap.wanted),
  818. le32_to_cpu(info->cap.seq),
  819. le32_to_cpu(info->cap.mseq),
  820. le64_to_cpu(info->cap.realm),
  821. info->cap.flags, &new_cap);
  822. /* set dir completion flag? */
  823. if (S_ISDIR(inode->i_mode) &&
  824. ci->i_files == 0 && ci->i_subdirs == 0 &&
  825. (caps & CEPH_CAP_FILE_SHARED) &&
  826. (issued & CEPH_CAP_FILE_EXCL) == 0 &&
  827. !__ceph_dir_is_complete(ci)) {
  828. dout(" marking %p complete (empty)\n", inode);
  829. i_size_write(inode, 0);
  830. __ceph_dir_set_complete(ci,
  831. atomic64_read(&ci->i_release_count),
  832. atomic64_read(&ci->i_ordered_count));
  833. }
  834. wake = true;
  835. } else {
  836. dout(" %p got snap_caps %s\n", inode,
  837. ceph_cap_string(le32_to_cpu(info->cap.caps)));
  838. ci->i_snap_caps |= le32_to_cpu(info->cap.caps);
  839. if (cap_fmode >= 0)
  840. __ceph_get_fmode(ci, cap_fmode);
  841. }
  842. } else if (cap_fmode >= 0) {
  843. pr_warn("mds issued no caps on %llx.%llx\n",
  844. ceph_vinop(inode));
  845. __ceph_get_fmode(ci, cap_fmode);
  846. }
  847. if (iinfo->inline_version > 0 &&
  848. iinfo->inline_version >= ci->i_inline_version) {
  849. int cache_caps = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
  850. ci->i_inline_version = iinfo->inline_version;
  851. if (ci->i_inline_version != CEPH_INLINE_NONE &&
  852. (locked_page ||
  853. (le32_to_cpu(info->cap.caps) & cache_caps)))
  854. fill_inline = true;
  855. }
  856. spin_unlock(&ci->i_ceph_lock);
  857. if (fill_inline)
  858. ceph_fill_inline_data(inode, locked_page,
  859. iinfo->inline_data, iinfo->inline_len);
  860. if (wake)
  861. wake_up_all(&ci->i_cap_wq);
  862. /* queue truncate if we saw i_size decrease */
  863. if (queue_trunc)
  864. ceph_queue_vmtruncate(inode);
  865. /* populate frag tree */
  866. if (S_ISDIR(inode->i_mode))
  867. ceph_fill_fragtree(inode, &info->fragtree, dirinfo);
  868. /* update delegation info? */
  869. if (dirinfo)
  870. ceph_fill_dirfrag(inode, dirinfo);
  871. err = 0;
  872. out:
  873. if (new_cap)
  874. ceph_put_cap(mdsc, new_cap);
  875. if (xattr_blob)
  876. ceph_buffer_put(xattr_blob);
  877. ceph_put_string(pool_ns);
  878. return err;
  879. }
  880. /*
  881. * caller should hold session s_mutex.
  882. */
  883. static void update_dentry_lease(struct dentry *dentry,
  884. struct ceph_mds_reply_lease *lease,
  885. struct ceph_mds_session *session,
  886. unsigned long from_time,
  887. struct ceph_vino *tgt_vino,
  888. struct ceph_vino *dir_vino)
  889. {
  890. struct ceph_dentry_info *di = ceph_dentry(dentry);
  891. long unsigned duration = le32_to_cpu(lease->duration_ms);
  892. long unsigned ttl = from_time + (duration * HZ) / 1000;
  893. long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
  894. struct inode *dir;
  895. struct ceph_mds_session *old_lease_session = NULL;
  896. /*
  897. * Make sure dentry's inode matches tgt_vino. NULL tgt_vino means that
  898. * we expect a negative dentry.
  899. */
  900. if (!tgt_vino && d_really_is_positive(dentry))
  901. return;
  902. if (tgt_vino && (d_really_is_negative(dentry) ||
  903. !ceph_ino_compare(d_inode(dentry), tgt_vino)))
  904. return;
  905. spin_lock(&dentry->d_lock);
  906. dout("update_dentry_lease %p duration %lu ms ttl %lu\n",
  907. dentry, duration, ttl);
  908. dir = d_inode(dentry->d_parent);
  909. /* make sure parent matches dir_vino */
  910. if (!ceph_ino_compare(dir, dir_vino))
  911. goto out_unlock;
  912. /* only track leases on regular dentries */
  913. if (ceph_snap(dir) != CEPH_NOSNAP)
  914. goto out_unlock;
  915. di->lease_shared_gen = ceph_inode(dir)->i_shared_gen;
  916. if (duration == 0)
  917. goto out_unlock;
  918. if (di->lease_gen == session->s_cap_gen &&
  919. time_before(ttl, di->time))
  920. goto out_unlock; /* we already have a newer lease. */
  921. if (di->lease_session && di->lease_session != session) {
  922. old_lease_session = di->lease_session;
  923. di->lease_session = NULL;
  924. }
  925. ceph_dentry_lru_touch(dentry);
  926. if (!di->lease_session)
  927. di->lease_session = ceph_get_mds_session(session);
  928. di->lease_gen = session->s_cap_gen;
  929. di->lease_seq = le32_to_cpu(lease->seq);
  930. di->lease_renew_after = half_ttl;
  931. di->lease_renew_from = 0;
  932. di->time = ttl;
  933. out_unlock:
  934. spin_unlock(&dentry->d_lock);
  935. if (old_lease_session)
  936. ceph_put_mds_session(old_lease_session);
  937. }
  938. /*
  939. * splice a dentry to an inode.
  940. * caller must hold directory i_mutex for this to be safe.
  941. */
  942. static struct dentry *splice_dentry(struct dentry *dn, struct inode *in)
  943. {
  944. struct dentry *realdn;
  945. BUG_ON(d_inode(dn));
  946. /* dn must be unhashed */
  947. if (!d_unhashed(dn))
  948. d_drop(dn);
  949. realdn = d_splice_alias(in, dn);
  950. if (IS_ERR(realdn)) {
  951. pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n",
  952. PTR_ERR(realdn), dn, in, ceph_vinop(in));
  953. dn = realdn; /* note realdn contains the error */
  954. goto out;
  955. } else if (realdn) {
  956. dout("dn %p (%d) spliced with %p (%d) "
  957. "inode %p ino %llx.%llx\n",
  958. dn, d_count(dn),
  959. realdn, d_count(realdn),
  960. d_inode(realdn), ceph_vinop(d_inode(realdn)));
  961. dput(dn);
  962. dn = realdn;
  963. } else {
  964. BUG_ON(!ceph_dentry(dn));
  965. dout("dn %p attached to %p ino %llx.%llx\n",
  966. dn, d_inode(dn), ceph_vinop(d_inode(dn)));
  967. }
  968. out:
  969. return dn;
  970. }
  971. /*
  972. * Incorporate results into the local cache. This is either just
  973. * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
  974. * after a lookup).
  975. *
  976. * A reply may contain
  977. * a directory inode along with a dentry.
  978. * and/or a target inode
  979. *
  980. * Called with snap_rwsem (read).
  981. */
  982. int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req)
  983. {
  984. struct ceph_mds_session *session = req->r_session;
  985. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  986. struct inode *in = NULL;
  987. struct ceph_vino tvino, dvino;
  988. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  989. int err = 0;
  990. dout("fill_trace %p is_dentry %d is_target %d\n", req,
  991. rinfo->head->is_dentry, rinfo->head->is_target);
  992. if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
  993. dout("fill_trace reply is empty!\n");
  994. if (rinfo->head->result == 0 && req->r_parent)
  995. ceph_invalidate_dir_request(req);
  996. return 0;
  997. }
  998. if (rinfo->head->is_dentry) {
  999. struct inode *dir = req->r_parent;
  1000. if (dir) {
  1001. err = fill_inode(dir, NULL,
  1002. &rinfo->diri, rinfo->dirfrag,
  1003. session, req->r_request_started, -1,
  1004. &req->r_caps_reservation);
  1005. if (err < 0)
  1006. goto done;
  1007. } else {
  1008. WARN_ON_ONCE(1);
  1009. }
  1010. if (dir && req->r_op == CEPH_MDS_OP_LOOKUPNAME) {
  1011. struct qstr dname;
  1012. struct dentry *dn, *parent;
  1013. BUG_ON(!rinfo->head->is_target);
  1014. BUG_ON(req->r_dentry);
  1015. parent = d_find_any_alias(dir);
  1016. BUG_ON(!parent);
  1017. dname.name = rinfo->dname;
  1018. dname.len = rinfo->dname_len;
  1019. dname.hash = full_name_hash(parent, dname.name, dname.len);
  1020. tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
  1021. tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
  1022. retry_lookup:
  1023. dn = d_lookup(parent, &dname);
  1024. dout("d_lookup on parent=%p name=%.*s got %p\n",
  1025. parent, dname.len, dname.name, dn);
  1026. if (!dn) {
  1027. dn = d_alloc(parent, &dname);
  1028. dout("d_alloc %p '%.*s' = %p\n", parent,
  1029. dname.len, dname.name, dn);
  1030. if (!dn) {
  1031. dput(parent);
  1032. err = -ENOMEM;
  1033. goto done;
  1034. }
  1035. err = 0;
  1036. } else if (d_really_is_positive(dn) &&
  1037. (ceph_ino(d_inode(dn)) != tvino.ino ||
  1038. ceph_snap(d_inode(dn)) != tvino.snap)) {
  1039. dout(" dn %p points to wrong inode %p\n",
  1040. dn, d_inode(dn));
  1041. d_delete(dn);
  1042. dput(dn);
  1043. goto retry_lookup;
  1044. }
  1045. req->r_dentry = dn;
  1046. dput(parent);
  1047. }
  1048. }
  1049. if (rinfo->head->is_target) {
  1050. tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
  1051. tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
  1052. in = ceph_get_inode(sb, tvino);
  1053. if (IS_ERR(in)) {
  1054. err = PTR_ERR(in);
  1055. goto done;
  1056. }
  1057. req->r_target_inode = in;
  1058. err = fill_inode(in, req->r_locked_page, &rinfo->targeti, NULL,
  1059. session, req->r_request_started,
  1060. (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
  1061. rinfo->head->result == 0) ? req->r_fmode : -1,
  1062. &req->r_caps_reservation);
  1063. if (err < 0) {
  1064. pr_err("fill_inode badness %p %llx.%llx\n",
  1065. in, ceph_vinop(in));
  1066. goto done;
  1067. }
  1068. }
  1069. /*
  1070. * ignore null lease/binding on snapdir ENOENT, or else we
  1071. * will have trouble splicing in the virtual snapdir later
  1072. */
  1073. if (rinfo->head->is_dentry &&
  1074. !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
  1075. test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
  1076. (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
  1077. fsc->mount_options->snapdir_name,
  1078. req->r_dentry->d_name.len))) {
  1079. /*
  1080. * lookup link rename : null -> possibly existing inode
  1081. * mknod symlink mkdir : null -> new inode
  1082. * unlink : linked -> null
  1083. */
  1084. struct inode *dir = req->r_parent;
  1085. struct dentry *dn = req->r_dentry;
  1086. bool have_dir_cap, have_lease;
  1087. BUG_ON(!dn);
  1088. BUG_ON(!dir);
  1089. BUG_ON(d_inode(dn->d_parent) != dir);
  1090. dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
  1091. dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
  1092. BUG_ON(ceph_ino(dir) != dvino.ino);
  1093. BUG_ON(ceph_snap(dir) != dvino.snap);
  1094. /* do we have a lease on the whole dir? */
  1095. have_dir_cap =
  1096. (le32_to_cpu(rinfo->diri.in->cap.caps) &
  1097. CEPH_CAP_FILE_SHARED);
  1098. /* do we have a dn lease? */
  1099. have_lease = have_dir_cap ||
  1100. le32_to_cpu(rinfo->dlease->duration_ms);
  1101. if (!have_lease)
  1102. dout("fill_trace no dentry lease or dir cap\n");
  1103. /* rename? */
  1104. if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
  1105. struct inode *olddir = req->r_old_dentry_dir;
  1106. BUG_ON(!olddir);
  1107. dout(" src %p '%pd' dst %p '%pd'\n",
  1108. req->r_old_dentry,
  1109. req->r_old_dentry,
  1110. dn, dn);
  1111. dout("fill_trace doing d_move %p -> %p\n",
  1112. req->r_old_dentry, dn);
  1113. /* d_move screws up sibling dentries' offsets */
  1114. ceph_dir_clear_ordered(dir);
  1115. ceph_dir_clear_ordered(olddir);
  1116. d_move(req->r_old_dentry, dn);
  1117. dout(" src %p '%pd' dst %p '%pd'\n",
  1118. req->r_old_dentry,
  1119. req->r_old_dentry,
  1120. dn, dn);
  1121. /* ensure target dentry is invalidated, despite
  1122. rehashing bug in vfs_rename_dir */
  1123. ceph_invalidate_dentry_lease(dn);
  1124. dout("dn %p gets new offset %lld\n", req->r_old_dentry,
  1125. ceph_dentry(req->r_old_dentry)->offset);
  1126. dn = req->r_old_dentry; /* use old_dentry */
  1127. }
  1128. /* null dentry? */
  1129. if (!rinfo->head->is_target) {
  1130. dout("fill_trace null dentry\n");
  1131. if (d_really_is_positive(dn)) {
  1132. ceph_dir_clear_ordered(dir);
  1133. dout("d_delete %p\n", dn);
  1134. d_delete(dn);
  1135. } else if (have_lease) {
  1136. if (d_unhashed(dn))
  1137. d_add(dn, NULL);
  1138. update_dentry_lease(dn, rinfo->dlease,
  1139. session,
  1140. req->r_request_started,
  1141. NULL, &dvino);
  1142. }
  1143. goto done;
  1144. }
  1145. /* attach proper inode */
  1146. if (d_really_is_negative(dn)) {
  1147. ceph_dir_clear_ordered(dir);
  1148. ihold(in);
  1149. dn = splice_dentry(dn, in);
  1150. if (IS_ERR(dn)) {
  1151. err = PTR_ERR(dn);
  1152. goto done;
  1153. }
  1154. req->r_dentry = dn; /* may have spliced */
  1155. } else if (d_really_is_positive(dn) && d_inode(dn) != in) {
  1156. dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
  1157. dn, d_inode(dn), ceph_vinop(d_inode(dn)),
  1158. ceph_vinop(in));
  1159. d_invalidate(dn);
  1160. have_lease = false;
  1161. }
  1162. if (have_lease) {
  1163. tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
  1164. tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
  1165. update_dentry_lease(dn, rinfo->dlease, session,
  1166. req->r_request_started,
  1167. &tvino, &dvino);
  1168. }
  1169. dout(" final dn %p\n", dn);
  1170. } else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
  1171. req->r_op == CEPH_MDS_OP_MKSNAP) &&
  1172. !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
  1173. struct dentry *dn = req->r_dentry;
  1174. struct inode *dir = req->r_parent;
  1175. /* fill out a snapdir LOOKUPSNAP dentry */
  1176. BUG_ON(!dn);
  1177. BUG_ON(!dir);
  1178. BUG_ON(ceph_snap(dir) != CEPH_SNAPDIR);
  1179. dout(" linking snapped dir %p to dn %p\n", in, dn);
  1180. ceph_dir_clear_ordered(dir);
  1181. ihold(in);
  1182. dn = splice_dentry(dn, in);
  1183. if (IS_ERR(dn)) {
  1184. err = PTR_ERR(dn);
  1185. goto done;
  1186. }
  1187. req->r_dentry = dn; /* may have spliced */
  1188. } else if (rinfo->head->is_dentry) {
  1189. struct ceph_vino *ptvino = NULL;
  1190. if ((le32_to_cpu(rinfo->diri.in->cap.caps) & CEPH_CAP_FILE_SHARED) ||
  1191. le32_to_cpu(rinfo->dlease->duration_ms)) {
  1192. dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
  1193. dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
  1194. if (rinfo->head->is_target) {
  1195. tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
  1196. tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
  1197. ptvino = &tvino;
  1198. }
  1199. update_dentry_lease(req->r_dentry, rinfo->dlease,
  1200. session, req->r_request_started, ptvino,
  1201. &dvino);
  1202. } else {
  1203. dout("%s: no dentry lease or dir cap\n", __func__);
  1204. }
  1205. }
  1206. done:
  1207. dout("fill_trace done err=%d\n", err);
  1208. return err;
  1209. }
  1210. /*
  1211. * Prepopulate our cache with readdir results, leases, etc.
  1212. */
  1213. static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req,
  1214. struct ceph_mds_session *session)
  1215. {
  1216. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  1217. int i, err = 0;
  1218. for (i = 0; i < rinfo->dir_nr; i++) {
  1219. struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
  1220. struct ceph_vino vino;
  1221. struct inode *in;
  1222. int rc;
  1223. vino.ino = le64_to_cpu(rde->inode.in->ino);
  1224. vino.snap = le64_to_cpu(rde->inode.in->snapid);
  1225. in = ceph_get_inode(req->r_dentry->d_sb, vino);
  1226. if (IS_ERR(in)) {
  1227. err = PTR_ERR(in);
  1228. dout("new_inode badness got %d\n", err);
  1229. continue;
  1230. }
  1231. rc = fill_inode(in, NULL, &rde->inode, NULL, session,
  1232. req->r_request_started, -1,
  1233. &req->r_caps_reservation);
  1234. if (rc < 0) {
  1235. pr_err("fill_inode badness on %p got %d\n", in, rc);
  1236. err = rc;
  1237. }
  1238. iput(in);
  1239. }
  1240. return err;
  1241. }
  1242. void ceph_readdir_cache_release(struct ceph_readdir_cache_control *ctl)
  1243. {
  1244. if (ctl->page) {
  1245. kunmap(ctl->page);
  1246. put_page(ctl->page);
  1247. ctl->page = NULL;
  1248. }
  1249. }
  1250. static int fill_readdir_cache(struct inode *dir, struct dentry *dn,
  1251. struct ceph_readdir_cache_control *ctl,
  1252. struct ceph_mds_request *req)
  1253. {
  1254. struct ceph_inode_info *ci = ceph_inode(dir);
  1255. unsigned nsize = PAGE_SIZE / sizeof(struct dentry*);
  1256. unsigned idx = ctl->index % nsize;
  1257. pgoff_t pgoff = ctl->index / nsize;
  1258. if (!ctl->page || pgoff != page_index(ctl->page)) {
  1259. ceph_readdir_cache_release(ctl);
  1260. if (idx == 0)
  1261. ctl->page = grab_cache_page(&dir->i_data, pgoff);
  1262. else
  1263. ctl->page = find_lock_page(&dir->i_data, pgoff);
  1264. if (!ctl->page) {
  1265. ctl->index = -1;
  1266. return idx == 0 ? -ENOMEM : 0;
  1267. }
  1268. /* reading/filling the cache are serialized by
  1269. * i_mutex, no need to use page lock */
  1270. unlock_page(ctl->page);
  1271. ctl->dentries = kmap(ctl->page);
  1272. if (idx == 0)
  1273. memset(ctl->dentries, 0, PAGE_SIZE);
  1274. }
  1275. if (req->r_dir_release_cnt == atomic64_read(&ci->i_release_count) &&
  1276. req->r_dir_ordered_cnt == atomic64_read(&ci->i_ordered_count)) {
  1277. dout("readdir cache dn %p idx %d\n", dn, ctl->index);
  1278. ctl->dentries[idx] = dn;
  1279. ctl->index++;
  1280. } else {
  1281. dout("disable readdir cache\n");
  1282. ctl->index = -1;
  1283. }
  1284. return 0;
  1285. }
  1286. int ceph_readdir_prepopulate(struct ceph_mds_request *req,
  1287. struct ceph_mds_session *session)
  1288. {
  1289. struct dentry *parent = req->r_dentry;
  1290. struct ceph_inode_info *ci = ceph_inode(d_inode(parent));
  1291. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  1292. struct qstr dname;
  1293. struct dentry *dn;
  1294. struct inode *in;
  1295. int err = 0, skipped = 0, ret, i;
  1296. struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
  1297. u32 frag = le32_to_cpu(rhead->args.readdir.frag);
  1298. u32 last_hash = 0;
  1299. u32 fpos_offset;
  1300. struct ceph_readdir_cache_control cache_ctl = {};
  1301. if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
  1302. return readdir_prepopulate_inodes_only(req, session);
  1303. if (rinfo->hash_order) {
  1304. if (req->r_path2) {
  1305. last_hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash,
  1306. req->r_path2,
  1307. strlen(req->r_path2));
  1308. last_hash = ceph_frag_value(last_hash);
  1309. } else if (rinfo->offset_hash) {
  1310. /* mds understands offset_hash */
  1311. WARN_ON_ONCE(req->r_readdir_offset != 2);
  1312. last_hash = le32_to_cpu(rhead->args.readdir.offset_hash);
  1313. }
  1314. }
  1315. if (rinfo->dir_dir &&
  1316. le32_to_cpu(rinfo->dir_dir->frag) != frag) {
  1317. dout("readdir_prepopulate got new frag %x -> %x\n",
  1318. frag, le32_to_cpu(rinfo->dir_dir->frag));
  1319. frag = le32_to_cpu(rinfo->dir_dir->frag);
  1320. if (!rinfo->hash_order)
  1321. req->r_readdir_offset = 2;
  1322. }
  1323. if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
  1324. dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
  1325. rinfo->dir_nr, parent);
  1326. } else {
  1327. dout("readdir_prepopulate %d items under dn %p\n",
  1328. rinfo->dir_nr, parent);
  1329. if (rinfo->dir_dir)
  1330. ceph_fill_dirfrag(d_inode(parent), rinfo->dir_dir);
  1331. if (ceph_frag_is_leftmost(frag) &&
  1332. req->r_readdir_offset == 2 &&
  1333. !(rinfo->hash_order && last_hash)) {
  1334. /* note dir version at start of readdir so we can
  1335. * tell if any dentries get dropped */
  1336. req->r_dir_release_cnt =
  1337. atomic64_read(&ci->i_release_count);
  1338. req->r_dir_ordered_cnt =
  1339. atomic64_read(&ci->i_ordered_count);
  1340. req->r_readdir_cache_idx = 0;
  1341. }
  1342. }
  1343. cache_ctl.index = req->r_readdir_cache_idx;
  1344. fpos_offset = req->r_readdir_offset;
  1345. /* FIXME: release caps/leases if error occurs */
  1346. for (i = 0; i < rinfo->dir_nr; i++) {
  1347. struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
  1348. struct ceph_vino tvino, dvino;
  1349. dname.name = rde->name;
  1350. dname.len = rde->name_len;
  1351. dname.hash = full_name_hash(parent, dname.name, dname.len);
  1352. tvino.ino = le64_to_cpu(rde->inode.in->ino);
  1353. tvino.snap = le64_to_cpu(rde->inode.in->snapid);
  1354. if (rinfo->hash_order) {
  1355. u32 hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash,
  1356. rde->name, rde->name_len);
  1357. hash = ceph_frag_value(hash);
  1358. if (hash != last_hash)
  1359. fpos_offset = 2;
  1360. last_hash = hash;
  1361. rde->offset = ceph_make_fpos(hash, fpos_offset++, true);
  1362. } else {
  1363. rde->offset = ceph_make_fpos(frag, fpos_offset++, false);
  1364. }
  1365. retry_lookup:
  1366. dn = d_lookup(parent, &dname);
  1367. dout("d_lookup on parent=%p name=%.*s got %p\n",
  1368. parent, dname.len, dname.name, dn);
  1369. if (!dn) {
  1370. dn = d_alloc(parent, &dname);
  1371. dout("d_alloc %p '%.*s' = %p\n", parent,
  1372. dname.len, dname.name, dn);
  1373. if (!dn) {
  1374. dout("d_alloc badness\n");
  1375. err = -ENOMEM;
  1376. goto out;
  1377. }
  1378. } else if (d_really_is_positive(dn) &&
  1379. (ceph_ino(d_inode(dn)) != tvino.ino ||
  1380. ceph_snap(d_inode(dn)) != tvino.snap)) {
  1381. dout(" dn %p points to wrong inode %p\n",
  1382. dn, d_inode(dn));
  1383. d_delete(dn);
  1384. dput(dn);
  1385. goto retry_lookup;
  1386. }
  1387. /* inode */
  1388. if (d_really_is_positive(dn)) {
  1389. in = d_inode(dn);
  1390. } else {
  1391. in = ceph_get_inode(parent->d_sb, tvino);
  1392. if (IS_ERR(in)) {
  1393. dout("new_inode badness\n");
  1394. d_drop(dn);
  1395. dput(dn);
  1396. err = PTR_ERR(in);
  1397. goto out;
  1398. }
  1399. }
  1400. ret = fill_inode(in, NULL, &rde->inode, NULL, session,
  1401. req->r_request_started, -1,
  1402. &req->r_caps_reservation);
  1403. if (ret < 0) {
  1404. pr_err("fill_inode badness on %p\n", in);
  1405. if (d_really_is_negative(dn))
  1406. iput(in);
  1407. d_drop(dn);
  1408. err = ret;
  1409. goto next_item;
  1410. }
  1411. if (d_really_is_negative(dn)) {
  1412. struct dentry *realdn;
  1413. if (ceph_security_xattr_deadlock(in)) {
  1414. dout(" skip splicing dn %p to inode %p"
  1415. " (security xattr deadlock)\n", dn, in);
  1416. iput(in);
  1417. skipped++;
  1418. goto next_item;
  1419. }
  1420. realdn = splice_dentry(dn, in);
  1421. if (IS_ERR(realdn)) {
  1422. err = PTR_ERR(realdn);
  1423. d_drop(dn);
  1424. dn = NULL;
  1425. goto next_item;
  1426. }
  1427. dn = realdn;
  1428. }
  1429. ceph_dentry(dn)->offset = rde->offset;
  1430. dvino = ceph_vino(d_inode(parent));
  1431. update_dentry_lease(dn, rde->lease, req->r_session,
  1432. req->r_request_started, &tvino, &dvino);
  1433. if (err == 0 && skipped == 0 && cache_ctl.index >= 0) {
  1434. ret = fill_readdir_cache(d_inode(parent), dn,
  1435. &cache_ctl, req);
  1436. if (ret < 0)
  1437. err = ret;
  1438. }
  1439. next_item:
  1440. if (dn)
  1441. dput(dn);
  1442. }
  1443. out:
  1444. if (err == 0 && skipped == 0) {
  1445. set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags);
  1446. req->r_readdir_cache_idx = cache_ctl.index;
  1447. }
  1448. ceph_readdir_cache_release(&cache_ctl);
  1449. dout("readdir_prepopulate done\n");
  1450. return err;
  1451. }
  1452. bool ceph_inode_set_size(struct inode *inode, loff_t size)
  1453. {
  1454. struct ceph_inode_info *ci = ceph_inode(inode);
  1455. bool ret;
  1456. spin_lock(&ci->i_ceph_lock);
  1457. dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
  1458. i_size_write(inode, size);
  1459. inode->i_blocks = calc_inode_blocks(size);
  1460. ret = __ceph_should_report_size(ci);
  1461. spin_unlock(&ci->i_ceph_lock);
  1462. return ret;
  1463. }
  1464. /*
  1465. * Write back inode data in a worker thread. (This can't be done
  1466. * in the message handler context.)
  1467. */
  1468. void ceph_queue_writeback(struct inode *inode)
  1469. {
  1470. ihold(inode);
  1471. if (queue_work(ceph_inode_to_client(inode)->wb_wq,
  1472. &ceph_inode(inode)->i_wb_work)) {
  1473. dout("ceph_queue_writeback %p\n", inode);
  1474. } else {
  1475. dout("ceph_queue_writeback %p failed\n", inode);
  1476. iput(inode);
  1477. }
  1478. }
  1479. static void ceph_writeback_work(struct work_struct *work)
  1480. {
  1481. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1482. i_wb_work);
  1483. struct inode *inode = &ci->vfs_inode;
  1484. dout("writeback %p\n", inode);
  1485. filemap_fdatawrite(&inode->i_data);
  1486. iput(inode);
  1487. }
  1488. /*
  1489. * queue an async invalidation
  1490. */
  1491. void ceph_queue_invalidate(struct inode *inode)
  1492. {
  1493. ihold(inode);
  1494. if (queue_work(ceph_inode_to_client(inode)->pg_inv_wq,
  1495. &ceph_inode(inode)->i_pg_inv_work)) {
  1496. dout("ceph_queue_invalidate %p\n", inode);
  1497. } else {
  1498. dout("ceph_queue_invalidate %p failed\n", inode);
  1499. iput(inode);
  1500. }
  1501. }
  1502. /*
  1503. * Invalidate inode pages in a worker thread. (This can't be done
  1504. * in the message handler context.)
  1505. */
  1506. static void ceph_invalidate_work(struct work_struct *work)
  1507. {
  1508. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1509. i_pg_inv_work);
  1510. struct inode *inode = &ci->vfs_inode;
  1511. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  1512. u32 orig_gen;
  1513. int check = 0;
  1514. mutex_lock(&ci->i_truncate_mutex);
  1515. if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
  1516. pr_warn_ratelimited("invalidate_pages %p %lld forced umount\n",
  1517. inode, ceph_ino(inode));
  1518. mapping_set_error(inode->i_mapping, -EIO);
  1519. truncate_pagecache(inode, 0);
  1520. mutex_unlock(&ci->i_truncate_mutex);
  1521. goto out;
  1522. }
  1523. spin_lock(&ci->i_ceph_lock);
  1524. dout("invalidate_pages %p gen %d revoking %d\n", inode,
  1525. ci->i_rdcache_gen, ci->i_rdcache_revoking);
  1526. if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
  1527. if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
  1528. check = 1;
  1529. spin_unlock(&ci->i_ceph_lock);
  1530. mutex_unlock(&ci->i_truncate_mutex);
  1531. goto out;
  1532. }
  1533. orig_gen = ci->i_rdcache_gen;
  1534. spin_unlock(&ci->i_ceph_lock);
  1535. if (invalidate_inode_pages2(inode->i_mapping) < 0) {
  1536. pr_err("invalidate_pages %p fails\n", inode);
  1537. }
  1538. spin_lock(&ci->i_ceph_lock);
  1539. if (orig_gen == ci->i_rdcache_gen &&
  1540. orig_gen == ci->i_rdcache_revoking) {
  1541. dout("invalidate_pages %p gen %d successful\n", inode,
  1542. ci->i_rdcache_gen);
  1543. ci->i_rdcache_revoking--;
  1544. check = 1;
  1545. } else {
  1546. dout("invalidate_pages %p gen %d raced, now %d revoking %d\n",
  1547. inode, orig_gen, ci->i_rdcache_gen,
  1548. ci->i_rdcache_revoking);
  1549. if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
  1550. check = 1;
  1551. }
  1552. spin_unlock(&ci->i_ceph_lock);
  1553. mutex_unlock(&ci->i_truncate_mutex);
  1554. out:
  1555. if (check)
  1556. ceph_check_caps(ci, 0, NULL);
  1557. iput(inode);
  1558. }
  1559. /*
  1560. * called by trunc_wq;
  1561. *
  1562. * We also truncate in a separate thread as well.
  1563. */
  1564. static void ceph_vmtruncate_work(struct work_struct *work)
  1565. {
  1566. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1567. i_vmtruncate_work);
  1568. struct inode *inode = &ci->vfs_inode;
  1569. dout("vmtruncate_work %p\n", inode);
  1570. __ceph_do_pending_vmtruncate(inode);
  1571. iput(inode);
  1572. }
  1573. /*
  1574. * Queue an async vmtruncate. If we fail to queue work, we will handle
  1575. * the truncation the next time we call __ceph_do_pending_vmtruncate.
  1576. */
  1577. void ceph_queue_vmtruncate(struct inode *inode)
  1578. {
  1579. struct ceph_inode_info *ci = ceph_inode(inode);
  1580. ihold(inode);
  1581. if (queue_work(ceph_sb_to_client(inode->i_sb)->trunc_wq,
  1582. &ci->i_vmtruncate_work)) {
  1583. dout("ceph_queue_vmtruncate %p\n", inode);
  1584. } else {
  1585. dout("ceph_queue_vmtruncate %p failed, pending=%d\n",
  1586. inode, ci->i_truncate_pending);
  1587. iput(inode);
  1588. }
  1589. }
  1590. /*
  1591. * Make sure any pending truncation is applied before doing anything
  1592. * that may depend on it.
  1593. */
  1594. void __ceph_do_pending_vmtruncate(struct inode *inode)
  1595. {
  1596. struct ceph_inode_info *ci = ceph_inode(inode);
  1597. u64 to;
  1598. int wrbuffer_refs, finish = 0;
  1599. mutex_lock(&ci->i_truncate_mutex);
  1600. retry:
  1601. spin_lock(&ci->i_ceph_lock);
  1602. if (ci->i_truncate_pending == 0) {
  1603. dout("__do_pending_vmtruncate %p none pending\n", inode);
  1604. spin_unlock(&ci->i_ceph_lock);
  1605. mutex_unlock(&ci->i_truncate_mutex);
  1606. return;
  1607. }
  1608. /*
  1609. * make sure any dirty snapped pages are flushed before we
  1610. * possibly truncate them.. so write AND block!
  1611. */
  1612. if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
  1613. struct ceph_cap_snap *capsnap;
  1614. to = ci->i_truncate_size;
  1615. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  1616. // MDS should have revoked Frw caps
  1617. WARN_ON_ONCE(capsnap->writing);
  1618. if (capsnap->dirty_pages && capsnap->size > to)
  1619. to = capsnap->size;
  1620. }
  1621. spin_unlock(&ci->i_ceph_lock);
  1622. dout("__do_pending_vmtruncate %p flushing snaps first\n",
  1623. inode);
  1624. truncate_pagecache(inode, to);
  1625. filemap_write_and_wait_range(&inode->i_data, 0,
  1626. inode->i_sb->s_maxbytes);
  1627. goto retry;
  1628. }
  1629. /* there should be no reader or writer */
  1630. WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref);
  1631. to = ci->i_truncate_size;
  1632. wrbuffer_refs = ci->i_wrbuffer_ref;
  1633. dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
  1634. ci->i_truncate_pending, to);
  1635. spin_unlock(&ci->i_ceph_lock);
  1636. truncate_pagecache(inode, to);
  1637. spin_lock(&ci->i_ceph_lock);
  1638. if (to == ci->i_truncate_size) {
  1639. ci->i_truncate_pending = 0;
  1640. finish = 1;
  1641. }
  1642. spin_unlock(&ci->i_ceph_lock);
  1643. if (!finish)
  1644. goto retry;
  1645. mutex_unlock(&ci->i_truncate_mutex);
  1646. if (wrbuffer_refs == 0)
  1647. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  1648. wake_up_all(&ci->i_cap_wq);
  1649. }
  1650. /*
  1651. * symlinks
  1652. */
  1653. static const struct inode_operations ceph_symlink_iops = {
  1654. .get_link = simple_get_link,
  1655. .setattr = ceph_setattr,
  1656. .getattr = ceph_getattr,
  1657. .listxattr = ceph_listxattr,
  1658. };
  1659. int __ceph_setattr(struct inode *inode, struct iattr *attr)
  1660. {
  1661. struct ceph_inode_info *ci = ceph_inode(inode);
  1662. const unsigned int ia_valid = attr->ia_valid;
  1663. struct ceph_mds_request *req;
  1664. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  1665. struct ceph_cap_flush *prealloc_cf;
  1666. int issued;
  1667. int release = 0, dirtied = 0;
  1668. int mask = 0;
  1669. int err = 0;
  1670. int inode_dirty_flags = 0;
  1671. bool lock_snap_rwsem = false;
  1672. prealloc_cf = ceph_alloc_cap_flush();
  1673. if (!prealloc_cf)
  1674. return -ENOMEM;
  1675. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
  1676. USE_AUTH_MDS);
  1677. if (IS_ERR(req)) {
  1678. ceph_free_cap_flush(prealloc_cf);
  1679. return PTR_ERR(req);
  1680. }
  1681. spin_lock(&ci->i_ceph_lock);
  1682. issued = __ceph_caps_issued(ci, NULL);
  1683. if (!ci->i_head_snapc &&
  1684. (issued & (CEPH_CAP_ANY_EXCL | CEPH_CAP_FILE_WR))) {
  1685. lock_snap_rwsem = true;
  1686. if (!down_read_trylock(&mdsc->snap_rwsem)) {
  1687. spin_unlock(&ci->i_ceph_lock);
  1688. down_read(&mdsc->snap_rwsem);
  1689. spin_lock(&ci->i_ceph_lock);
  1690. issued = __ceph_caps_issued(ci, NULL);
  1691. }
  1692. }
  1693. dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
  1694. if (ia_valid & ATTR_UID) {
  1695. dout("setattr %p uid %d -> %d\n", inode,
  1696. from_kuid(&init_user_ns, inode->i_uid),
  1697. from_kuid(&init_user_ns, attr->ia_uid));
  1698. if (issued & CEPH_CAP_AUTH_EXCL) {
  1699. inode->i_uid = attr->ia_uid;
  1700. dirtied |= CEPH_CAP_AUTH_EXCL;
  1701. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1702. !uid_eq(attr->ia_uid, inode->i_uid)) {
  1703. req->r_args.setattr.uid = cpu_to_le32(
  1704. from_kuid(&init_user_ns, attr->ia_uid));
  1705. mask |= CEPH_SETATTR_UID;
  1706. release |= CEPH_CAP_AUTH_SHARED;
  1707. }
  1708. }
  1709. if (ia_valid & ATTR_GID) {
  1710. dout("setattr %p gid %d -> %d\n", inode,
  1711. from_kgid(&init_user_ns, inode->i_gid),
  1712. from_kgid(&init_user_ns, attr->ia_gid));
  1713. if (issued & CEPH_CAP_AUTH_EXCL) {
  1714. inode->i_gid = attr->ia_gid;
  1715. dirtied |= CEPH_CAP_AUTH_EXCL;
  1716. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1717. !gid_eq(attr->ia_gid, inode->i_gid)) {
  1718. req->r_args.setattr.gid = cpu_to_le32(
  1719. from_kgid(&init_user_ns, attr->ia_gid));
  1720. mask |= CEPH_SETATTR_GID;
  1721. release |= CEPH_CAP_AUTH_SHARED;
  1722. }
  1723. }
  1724. if (ia_valid & ATTR_MODE) {
  1725. dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
  1726. attr->ia_mode);
  1727. if (issued & CEPH_CAP_AUTH_EXCL) {
  1728. inode->i_mode = attr->ia_mode;
  1729. dirtied |= CEPH_CAP_AUTH_EXCL;
  1730. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1731. attr->ia_mode != inode->i_mode) {
  1732. inode->i_mode = attr->ia_mode;
  1733. req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
  1734. mask |= CEPH_SETATTR_MODE;
  1735. release |= CEPH_CAP_AUTH_SHARED;
  1736. }
  1737. }
  1738. if (ia_valid & ATTR_ATIME) {
  1739. dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode,
  1740. inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
  1741. attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
  1742. if (issued & CEPH_CAP_FILE_EXCL) {
  1743. ci->i_time_warp_seq++;
  1744. inode->i_atime = attr->ia_atime;
  1745. dirtied |= CEPH_CAP_FILE_EXCL;
  1746. } else if ((issued & CEPH_CAP_FILE_WR) &&
  1747. timespec_compare(&inode->i_atime,
  1748. &attr->ia_atime) < 0) {
  1749. inode->i_atime = attr->ia_atime;
  1750. dirtied |= CEPH_CAP_FILE_WR;
  1751. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1752. !timespec_equal(&inode->i_atime, &attr->ia_atime)) {
  1753. ceph_encode_timespec(&req->r_args.setattr.atime,
  1754. &attr->ia_atime);
  1755. mask |= CEPH_SETATTR_ATIME;
  1756. release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD |
  1757. CEPH_CAP_FILE_WR;
  1758. }
  1759. }
  1760. if (ia_valid & ATTR_MTIME) {
  1761. dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode,
  1762. inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
  1763. attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
  1764. if (issued & CEPH_CAP_FILE_EXCL) {
  1765. ci->i_time_warp_seq++;
  1766. inode->i_mtime = attr->ia_mtime;
  1767. dirtied |= CEPH_CAP_FILE_EXCL;
  1768. } else if ((issued & CEPH_CAP_FILE_WR) &&
  1769. timespec_compare(&inode->i_mtime,
  1770. &attr->ia_mtime) < 0) {
  1771. inode->i_mtime = attr->ia_mtime;
  1772. dirtied |= CEPH_CAP_FILE_WR;
  1773. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1774. !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) {
  1775. ceph_encode_timespec(&req->r_args.setattr.mtime,
  1776. &attr->ia_mtime);
  1777. mask |= CEPH_SETATTR_MTIME;
  1778. release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
  1779. CEPH_CAP_FILE_WR;
  1780. }
  1781. }
  1782. if (ia_valid & ATTR_SIZE) {
  1783. dout("setattr %p size %lld -> %lld\n", inode,
  1784. inode->i_size, attr->ia_size);
  1785. if ((issued & CEPH_CAP_FILE_EXCL) &&
  1786. attr->ia_size > inode->i_size) {
  1787. i_size_write(inode, attr->ia_size);
  1788. inode->i_blocks = calc_inode_blocks(attr->ia_size);
  1789. ci->i_reported_size = attr->ia_size;
  1790. dirtied |= CEPH_CAP_FILE_EXCL;
  1791. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1792. attr->ia_size != inode->i_size) {
  1793. req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
  1794. req->r_args.setattr.old_size =
  1795. cpu_to_le64(inode->i_size);
  1796. mask |= CEPH_SETATTR_SIZE;
  1797. release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
  1798. CEPH_CAP_FILE_WR;
  1799. }
  1800. }
  1801. /* these do nothing */
  1802. if (ia_valid & ATTR_CTIME) {
  1803. bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
  1804. ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
  1805. dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode,
  1806. inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
  1807. attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
  1808. only ? "ctime only" : "ignored");
  1809. if (only) {
  1810. /*
  1811. * if kernel wants to dirty ctime but nothing else,
  1812. * we need to choose a cap to dirty under, or do
  1813. * a almost-no-op setattr
  1814. */
  1815. if (issued & CEPH_CAP_AUTH_EXCL)
  1816. dirtied |= CEPH_CAP_AUTH_EXCL;
  1817. else if (issued & CEPH_CAP_FILE_EXCL)
  1818. dirtied |= CEPH_CAP_FILE_EXCL;
  1819. else if (issued & CEPH_CAP_XATTR_EXCL)
  1820. dirtied |= CEPH_CAP_XATTR_EXCL;
  1821. else
  1822. mask |= CEPH_SETATTR_CTIME;
  1823. }
  1824. }
  1825. if (ia_valid & ATTR_FILE)
  1826. dout("setattr %p ATTR_FILE ... hrm!\n", inode);
  1827. if (dirtied) {
  1828. inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied,
  1829. &prealloc_cf);
  1830. inode->i_ctime = attr->ia_ctime;
  1831. }
  1832. release &= issued;
  1833. spin_unlock(&ci->i_ceph_lock);
  1834. if (lock_snap_rwsem)
  1835. up_read(&mdsc->snap_rwsem);
  1836. if (inode_dirty_flags)
  1837. __mark_inode_dirty(inode, inode_dirty_flags);
  1838. if (mask) {
  1839. req->r_inode = inode;
  1840. ihold(inode);
  1841. req->r_inode_drop = release;
  1842. req->r_args.setattr.mask = cpu_to_le32(mask);
  1843. req->r_num_caps = 1;
  1844. req->r_stamp = attr->ia_ctime;
  1845. err = ceph_mdsc_do_request(mdsc, NULL, req);
  1846. }
  1847. dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
  1848. ceph_cap_string(dirtied), mask);
  1849. ceph_mdsc_put_request(req);
  1850. ceph_free_cap_flush(prealloc_cf);
  1851. if (err >= 0 && (mask & CEPH_SETATTR_SIZE))
  1852. __ceph_do_pending_vmtruncate(inode);
  1853. return err;
  1854. }
  1855. /*
  1856. * setattr
  1857. */
  1858. int ceph_setattr(struct dentry *dentry, struct iattr *attr)
  1859. {
  1860. struct inode *inode = d_inode(dentry);
  1861. int err;
  1862. if (ceph_snap(inode) != CEPH_NOSNAP)
  1863. return -EROFS;
  1864. err = setattr_prepare(dentry, attr);
  1865. if (err != 0)
  1866. return err;
  1867. err = __ceph_setattr(inode, attr);
  1868. if (err >= 0 && (attr->ia_valid & ATTR_MODE))
  1869. err = posix_acl_chmod(inode, attr->ia_mode);
  1870. return err;
  1871. }
  1872. /*
  1873. * Verify that we have a lease on the given mask. If not,
  1874. * do a getattr against an mds.
  1875. */
  1876. int __ceph_do_getattr(struct inode *inode, struct page *locked_page,
  1877. int mask, bool force)
  1878. {
  1879. struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
  1880. struct ceph_mds_client *mdsc = fsc->mdsc;
  1881. struct ceph_mds_request *req;
  1882. int err;
  1883. if (ceph_snap(inode) == CEPH_SNAPDIR) {
  1884. dout("do_getattr inode %p SNAPDIR\n", inode);
  1885. return 0;
  1886. }
  1887. dout("do_getattr inode %p mask %s mode 0%o\n",
  1888. inode, ceph_cap_string(mask), inode->i_mode);
  1889. if (!force && ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
  1890. return 0;
  1891. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
  1892. if (IS_ERR(req))
  1893. return PTR_ERR(req);
  1894. req->r_inode = inode;
  1895. ihold(inode);
  1896. req->r_num_caps = 1;
  1897. req->r_args.getattr.mask = cpu_to_le32(mask);
  1898. req->r_locked_page = locked_page;
  1899. err = ceph_mdsc_do_request(mdsc, NULL, req);
  1900. if (locked_page && err == 0) {
  1901. u64 inline_version = req->r_reply_info.targeti.inline_version;
  1902. if (inline_version == 0) {
  1903. /* the reply is supposed to contain inline data */
  1904. err = -EINVAL;
  1905. } else if (inline_version == CEPH_INLINE_NONE) {
  1906. err = -ENODATA;
  1907. } else {
  1908. err = req->r_reply_info.targeti.inline_len;
  1909. }
  1910. }
  1911. ceph_mdsc_put_request(req);
  1912. dout("do_getattr result=%d\n", err);
  1913. return err;
  1914. }
  1915. /*
  1916. * Check inode permissions. We verify we have a valid value for
  1917. * the AUTH cap, then call the generic handler.
  1918. */
  1919. int ceph_permission(struct inode *inode, int mask)
  1920. {
  1921. int err;
  1922. if (mask & MAY_NOT_BLOCK)
  1923. return -ECHILD;
  1924. err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, false);
  1925. if (!err)
  1926. err = generic_permission(inode, mask);
  1927. return err;
  1928. }
  1929. /*
  1930. * Get all attributes. Hopefully somedata we'll have a statlite()
  1931. * and can limit the fields we require to be accurate.
  1932. */
  1933. int ceph_getattr(const struct path *path, struct kstat *stat,
  1934. u32 request_mask, unsigned int flags)
  1935. {
  1936. struct inode *inode = d_inode(path->dentry);
  1937. struct ceph_inode_info *ci = ceph_inode(inode);
  1938. int err;
  1939. err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL, false);
  1940. if (!err) {
  1941. generic_fillattr(inode, stat);
  1942. stat->ino = ceph_translate_ino(inode->i_sb, inode->i_ino);
  1943. if (ceph_snap(inode) != CEPH_NOSNAP)
  1944. stat->dev = ceph_snap(inode);
  1945. else
  1946. stat->dev = 0;
  1947. if (S_ISDIR(inode->i_mode)) {
  1948. if (ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb),
  1949. RBYTES))
  1950. stat->size = ci->i_rbytes;
  1951. else
  1952. stat->size = ci->i_files + ci->i_subdirs;
  1953. stat->blocks = 0;
  1954. stat->blksize = 65536;
  1955. }
  1956. }
  1957. return err;
  1958. }