inode.c 69 KB

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