inode.c 63 KB

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