inode.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271
  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. kmem_cache_free(ceph_inode_cachep, ci);
  464. }
  465. void ceph_destroy_inode(struct inode *inode)
  466. {
  467. struct ceph_inode_info *ci = ceph_inode(inode);
  468. struct ceph_inode_frag *frag;
  469. struct rb_node *n;
  470. dout("destroy_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
  471. ceph_fscache_unregister_inode_cookie(ci);
  472. ceph_queue_caps_release(inode);
  473. if (__ceph_has_any_quota(ci))
  474. ceph_adjust_quota_realms_count(inode, false);
  475. /*
  476. * we may still have a snap_realm reference if there are stray
  477. * caps in i_snap_caps.
  478. */
  479. if (ci->i_snap_realm) {
  480. struct ceph_mds_client *mdsc =
  481. ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
  482. struct ceph_snap_realm *realm = ci->i_snap_realm;
  483. dout(" dropping residual ref to snap realm %p\n", realm);
  484. spin_lock(&realm->inodes_with_caps_lock);
  485. list_del_init(&ci->i_snap_realm_item);
  486. ci->i_snap_realm = NULL;
  487. if (realm->ino == ci->i_vino.ino)
  488. realm->inode = NULL;
  489. spin_unlock(&realm->inodes_with_caps_lock);
  490. ceph_put_snap_realm(mdsc, realm);
  491. }
  492. kfree(ci->i_symlink);
  493. while ((n = rb_first(&ci->i_fragtree)) != NULL) {
  494. frag = rb_entry(n, struct ceph_inode_frag, node);
  495. rb_erase(n, &ci->i_fragtree);
  496. kfree(frag);
  497. }
  498. ci->i_fragtree_nsplits = 0;
  499. __ceph_destroy_xattrs(ci);
  500. if (ci->i_xattrs.blob)
  501. ceph_buffer_put(ci->i_xattrs.blob);
  502. if (ci->i_xattrs.prealloc_blob)
  503. ceph_buffer_put(ci->i_xattrs.prealloc_blob);
  504. ceph_put_string(rcu_dereference_raw(ci->i_layout.pool_ns));
  505. call_rcu(&inode->i_rcu, ceph_i_callback);
  506. }
  507. int ceph_drop_inode(struct inode *inode)
  508. {
  509. /*
  510. * Positve dentry and corresponding inode are always accompanied
  511. * in MDS reply. So no need to keep inode in the cache after
  512. * dropping all its aliases.
  513. */
  514. return 1;
  515. }
  516. static inline blkcnt_t calc_inode_blocks(u64 size)
  517. {
  518. return (size + (1<<9) - 1) >> 9;
  519. }
  520. /*
  521. * Helpers to fill in size, ctime, mtime, and atime. We have to be
  522. * careful because either the client or MDS may have more up to date
  523. * info, depending on which capabilities are held, and whether
  524. * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
  525. * and size are monotonically increasing, except when utimes() or
  526. * truncate() increments the corresponding _seq values.)
  527. */
  528. int ceph_fill_file_size(struct inode *inode, int issued,
  529. u32 truncate_seq, u64 truncate_size, u64 size)
  530. {
  531. struct ceph_inode_info *ci = ceph_inode(inode);
  532. int queue_trunc = 0;
  533. if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
  534. (truncate_seq == ci->i_truncate_seq && size > inode->i_size)) {
  535. dout("size %lld -> %llu\n", inode->i_size, size);
  536. if (size > 0 && S_ISDIR(inode->i_mode)) {
  537. pr_err("fill_file_size non-zero size for directory\n");
  538. size = 0;
  539. }
  540. i_size_write(inode, size);
  541. inode->i_blocks = calc_inode_blocks(size);
  542. ci->i_reported_size = size;
  543. if (truncate_seq != ci->i_truncate_seq) {
  544. dout("truncate_seq %u -> %u\n",
  545. ci->i_truncate_seq, truncate_seq);
  546. ci->i_truncate_seq = truncate_seq;
  547. /* the MDS should have revoked these caps */
  548. WARN_ON_ONCE(issued & (CEPH_CAP_FILE_EXCL |
  549. CEPH_CAP_FILE_RD |
  550. CEPH_CAP_FILE_WR |
  551. CEPH_CAP_FILE_LAZYIO));
  552. /*
  553. * If we hold relevant caps, or in the case where we're
  554. * not the only client referencing this file and we
  555. * don't hold those caps, then we need to check whether
  556. * the file is either opened or mmaped
  557. */
  558. if ((issued & (CEPH_CAP_FILE_CACHE|
  559. CEPH_CAP_FILE_BUFFER)) ||
  560. mapping_mapped(inode->i_mapping) ||
  561. __ceph_caps_file_wanted(ci)) {
  562. ci->i_truncate_pending++;
  563. queue_trunc = 1;
  564. }
  565. }
  566. }
  567. if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
  568. ci->i_truncate_size != truncate_size) {
  569. dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
  570. truncate_size);
  571. ci->i_truncate_size = truncate_size;
  572. }
  573. if (queue_trunc)
  574. ceph_fscache_invalidate(inode);
  575. return queue_trunc;
  576. }
  577. void ceph_fill_file_time(struct inode *inode, int issued,
  578. u64 time_warp_seq, struct timespec *ctime,
  579. struct timespec *mtime, struct timespec *atime)
  580. {
  581. struct ceph_inode_info *ci = ceph_inode(inode);
  582. struct timespec64 ctime64 = timespec_to_timespec64(*ctime);
  583. struct timespec64 mtime64 = timespec_to_timespec64(*mtime);
  584. struct timespec64 atime64 = timespec_to_timespec64(*atime);
  585. int warn = 0;
  586. if (issued & (CEPH_CAP_FILE_EXCL|
  587. CEPH_CAP_FILE_WR|
  588. CEPH_CAP_FILE_BUFFER|
  589. CEPH_CAP_AUTH_EXCL|
  590. CEPH_CAP_XATTR_EXCL)) {
  591. if (ci->i_version == 0 ||
  592. timespec64_compare(&ctime64, &inode->i_ctime) > 0) {
  593. dout("ctime %lld.%09ld -> %lld.%09ld inc w/ cap\n",
  594. (long long)inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
  595. (long long)ctime->tv_sec, ctime->tv_nsec);
  596. inode->i_ctime = ctime64;
  597. }
  598. if (ci->i_version == 0 ||
  599. ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
  600. /* the MDS did a utimes() */
  601. dout("mtime %lld.%09ld -> %lld.%09ld "
  602. "tw %d -> %d\n",
  603. (long long)inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
  604. (long long)mtime->tv_sec, mtime->tv_nsec,
  605. ci->i_time_warp_seq, (int)time_warp_seq);
  606. inode->i_mtime = mtime64;
  607. inode->i_atime = atime64;
  608. ci->i_time_warp_seq = time_warp_seq;
  609. } else if (time_warp_seq == ci->i_time_warp_seq) {
  610. /* nobody did utimes(); take the max */
  611. if (timespec64_compare(&mtime64, &inode->i_mtime) > 0) {
  612. dout("mtime %lld.%09ld -> %lld.%09ld inc\n",
  613. (long long)inode->i_mtime.tv_sec,
  614. inode->i_mtime.tv_nsec,
  615. (long long)mtime->tv_sec, mtime->tv_nsec);
  616. inode->i_mtime = mtime64;
  617. }
  618. if (timespec64_compare(&atime64, &inode->i_atime) > 0) {
  619. dout("atime %lld.%09ld -> %lld.%09ld inc\n",
  620. (long long)inode->i_atime.tv_sec,
  621. inode->i_atime.tv_nsec,
  622. (long long)atime->tv_sec, atime->tv_nsec);
  623. inode->i_atime = atime64;
  624. }
  625. } else if (issued & CEPH_CAP_FILE_EXCL) {
  626. /* we did a utimes(); ignore mds values */
  627. } else {
  628. warn = 1;
  629. }
  630. } else {
  631. /* we have no write|excl caps; whatever the MDS says is true */
  632. if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
  633. inode->i_ctime = ctime64;
  634. inode->i_mtime = mtime64;
  635. inode->i_atime = atime64;
  636. ci->i_time_warp_seq = time_warp_seq;
  637. } else {
  638. warn = 1;
  639. }
  640. }
  641. if (warn) /* time_warp_seq shouldn't go backwards */
  642. dout("%p mds time_warp_seq %llu < %u\n",
  643. inode, time_warp_seq, ci->i_time_warp_seq);
  644. }
  645. /*
  646. * Populate an inode based on info from mds. May be called on new or
  647. * existing inodes.
  648. */
  649. static int fill_inode(struct inode *inode, struct page *locked_page,
  650. struct ceph_mds_reply_info_in *iinfo,
  651. struct ceph_mds_reply_dirfrag *dirinfo,
  652. struct ceph_mds_session *session,
  653. unsigned long ttl_from, int cap_fmode,
  654. struct ceph_cap_reservation *caps_reservation)
  655. {
  656. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  657. struct ceph_mds_reply_inode *info = iinfo->in;
  658. struct ceph_inode_info *ci = ceph_inode(inode);
  659. int issued = 0, implemented, new_issued;
  660. struct timespec mtime, atime, ctime;
  661. struct ceph_buffer *xattr_blob = NULL;
  662. struct ceph_string *pool_ns = NULL;
  663. struct ceph_cap *new_cap = NULL;
  664. int err = 0;
  665. bool wake = false;
  666. bool queue_trunc = false;
  667. bool new_version = false;
  668. bool fill_inline = false;
  669. dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
  670. inode, ceph_vinop(inode), le64_to_cpu(info->version),
  671. ci->i_version);
  672. /* prealloc new cap struct */
  673. if (info->cap.caps && ceph_snap(inode) == CEPH_NOSNAP)
  674. new_cap = ceph_get_cap(mdsc, caps_reservation);
  675. /*
  676. * prealloc xattr data, if it looks like we'll need it. only
  677. * if len > 4 (meaning there are actually xattrs; the first 4
  678. * bytes are the xattr count).
  679. */
  680. if (iinfo->xattr_len > 4) {
  681. xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
  682. if (!xattr_blob)
  683. pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
  684. iinfo->xattr_len);
  685. }
  686. if (iinfo->pool_ns_len > 0)
  687. pool_ns = ceph_find_or_create_string(iinfo->pool_ns_data,
  688. iinfo->pool_ns_len);
  689. spin_lock(&ci->i_ceph_lock);
  690. /*
  691. * provided version will be odd if inode value is projected,
  692. * even if stable. skip the update if we have newer stable
  693. * info (ours>=theirs, e.g. due to racing mds replies), unless
  694. * we are getting projected (unstable) info (in which case the
  695. * version is odd, and we want ours>theirs).
  696. * us them
  697. * 2 2 skip
  698. * 3 2 skip
  699. * 3 3 update
  700. */
  701. if (ci->i_version == 0 ||
  702. ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
  703. le64_to_cpu(info->version) > (ci->i_version & ~1)))
  704. new_version = true;
  705. issued = __ceph_caps_issued(ci, &implemented);
  706. issued |= implemented | __ceph_caps_dirty(ci);
  707. new_issued = ~issued & le32_to_cpu(info->cap.caps);
  708. /* update inode */
  709. inode->i_rdev = le32_to_cpu(info->rdev);
  710. inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
  711. __ceph_update_quota(ci, iinfo->max_bytes, iinfo->max_files);
  712. if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) &&
  713. (issued & CEPH_CAP_AUTH_EXCL) == 0) {
  714. inode->i_mode = le32_to_cpu(info->mode);
  715. inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid));
  716. inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid));
  717. dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
  718. from_kuid(&init_user_ns, inode->i_uid),
  719. from_kgid(&init_user_ns, inode->i_gid));
  720. }
  721. if ((new_version || (new_issued & CEPH_CAP_LINK_SHARED)) &&
  722. (issued & CEPH_CAP_LINK_EXCL) == 0)
  723. set_nlink(inode, le32_to_cpu(info->nlink));
  724. if (new_version || (new_issued & CEPH_CAP_ANY_RD)) {
  725. /* be careful with mtime, atime, size */
  726. ceph_decode_timespec(&atime, &info->atime);
  727. ceph_decode_timespec(&mtime, &info->mtime);
  728. ceph_decode_timespec(&ctime, &info->ctime);
  729. ceph_fill_file_time(inode, issued,
  730. le32_to_cpu(info->time_warp_seq),
  731. &ctime, &mtime, &atime);
  732. }
  733. if (new_version ||
  734. (new_issued & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR))) {
  735. s64 old_pool = ci->i_layout.pool_id;
  736. struct ceph_string *old_ns;
  737. ceph_file_layout_from_legacy(&ci->i_layout, &info->layout);
  738. old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
  739. lockdep_is_held(&ci->i_ceph_lock));
  740. rcu_assign_pointer(ci->i_layout.pool_ns, pool_ns);
  741. if (ci->i_layout.pool_id != old_pool || pool_ns != old_ns)
  742. ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
  743. pool_ns = old_ns;
  744. queue_trunc = ceph_fill_file_size(inode, issued,
  745. le32_to_cpu(info->truncate_seq),
  746. le64_to_cpu(info->truncate_size),
  747. le64_to_cpu(info->size));
  748. /* only update max_size on auth cap */
  749. if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
  750. ci->i_max_size != le64_to_cpu(info->max_size)) {
  751. dout("max_size %lld -> %llu\n", ci->i_max_size,
  752. le64_to_cpu(info->max_size));
  753. ci->i_max_size = le64_to_cpu(info->max_size);
  754. }
  755. }
  756. /* xattrs */
  757. /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
  758. if ((ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) &&
  759. le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
  760. if (ci->i_xattrs.blob)
  761. ceph_buffer_put(ci->i_xattrs.blob);
  762. ci->i_xattrs.blob = xattr_blob;
  763. if (xattr_blob)
  764. memcpy(ci->i_xattrs.blob->vec.iov_base,
  765. iinfo->xattr_data, iinfo->xattr_len);
  766. ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
  767. ceph_forget_all_cached_acls(inode);
  768. xattr_blob = NULL;
  769. }
  770. /* finally update i_version */
  771. ci->i_version = le64_to_cpu(info->version);
  772. inode->i_mapping->a_ops = &ceph_aops;
  773. switch (inode->i_mode & S_IFMT) {
  774. case S_IFIFO:
  775. case S_IFBLK:
  776. case S_IFCHR:
  777. case S_IFSOCK:
  778. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  779. inode->i_op = &ceph_file_iops;
  780. break;
  781. case S_IFREG:
  782. inode->i_op = &ceph_file_iops;
  783. inode->i_fop = &ceph_file_fops;
  784. break;
  785. case S_IFLNK:
  786. inode->i_op = &ceph_symlink_iops;
  787. if (!ci->i_symlink) {
  788. u32 symlen = iinfo->symlink_len;
  789. char *sym;
  790. spin_unlock(&ci->i_ceph_lock);
  791. if (symlen != i_size_read(inode)) {
  792. pr_err("fill_inode %llx.%llx BAD symlink "
  793. "size %lld\n", ceph_vinop(inode),
  794. i_size_read(inode));
  795. i_size_write(inode, symlen);
  796. inode->i_blocks = calc_inode_blocks(symlen);
  797. }
  798. err = -ENOMEM;
  799. sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS);
  800. if (!sym)
  801. goto out;
  802. spin_lock(&ci->i_ceph_lock);
  803. if (!ci->i_symlink)
  804. ci->i_symlink = sym;
  805. else
  806. kfree(sym); /* lost a race */
  807. }
  808. inode->i_link = ci->i_symlink;
  809. break;
  810. case S_IFDIR:
  811. inode->i_op = &ceph_dir_iops;
  812. inode->i_fop = &ceph_dir_fops;
  813. ci->i_dir_layout = iinfo->dir_layout;
  814. ci->i_files = le64_to_cpu(info->files);
  815. ci->i_subdirs = le64_to_cpu(info->subdirs);
  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. ceph_decode_timespec(&ci->i_rctime, &info->rctime);
  820. break;
  821. default:
  822. pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
  823. ceph_vinop(inode), inode->i_mode);
  824. }
  825. /* were we issued a capability? */
  826. if (info->cap.caps) {
  827. if (ceph_snap(inode) == CEPH_NOSNAP) {
  828. unsigned caps = le32_to_cpu(info->cap.caps);
  829. ceph_add_cap(inode, session,
  830. le64_to_cpu(info->cap.cap_id),
  831. cap_fmode, caps,
  832. le32_to_cpu(info->cap.wanted),
  833. le32_to_cpu(info->cap.seq),
  834. le32_to_cpu(info->cap.mseq),
  835. le64_to_cpu(info->cap.realm),
  836. info->cap.flags, &new_cap);
  837. /* set dir completion flag? */
  838. if (S_ISDIR(inode->i_mode) &&
  839. ci->i_files == 0 && ci->i_subdirs == 0 &&
  840. (caps & CEPH_CAP_FILE_SHARED) &&
  841. (issued & CEPH_CAP_FILE_EXCL) == 0 &&
  842. !__ceph_dir_is_complete(ci)) {
  843. dout(" marking %p complete (empty)\n", inode);
  844. i_size_write(inode, 0);
  845. __ceph_dir_set_complete(ci,
  846. atomic64_read(&ci->i_release_count),
  847. atomic64_read(&ci->i_ordered_count));
  848. }
  849. wake = true;
  850. } else {
  851. dout(" %p got snap_caps %s\n", inode,
  852. ceph_cap_string(le32_to_cpu(info->cap.caps)));
  853. ci->i_snap_caps |= le32_to_cpu(info->cap.caps);
  854. if (cap_fmode >= 0)
  855. __ceph_get_fmode(ci, cap_fmode);
  856. }
  857. } else if (cap_fmode >= 0) {
  858. pr_warn("mds issued no caps on %llx.%llx\n",
  859. ceph_vinop(inode));
  860. __ceph_get_fmode(ci, cap_fmode);
  861. }
  862. if (iinfo->inline_version > 0 &&
  863. iinfo->inline_version >= ci->i_inline_version) {
  864. int cache_caps = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
  865. ci->i_inline_version = iinfo->inline_version;
  866. if (ci->i_inline_version != CEPH_INLINE_NONE &&
  867. (locked_page ||
  868. (le32_to_cpu(info->cap.caps) & cache_caps)))
  869. fill_inline = true;
  870. }
  871. spin_unlock(&ci->i_ceph_lock);
  872. if (fill_inline)
  873. ceph_fill_inline_data(inode, locked_page,
  874. iinfo->inline_data, iinfo->inline_len);
  875. if (wake)
  876. wake_up_all(&ci->i_cap_wq);
  877. /* queue truncate if we saw i_size decrease */
  878. if (queue_trunc)
  879. ceph_queue_vmtruncate(inode);
  880. /* populate frag tree */
  881. if (S_ISDIR(inode->i_mode))
  882. ceph_fill_fragtree(inode, &info->fragtree, dirinfo);
  883. /* update delegation info? */
  884. if (dirinfo)
  885. ceph_fill_dirfrag(inode, dirinfo);
  886. err = 0;
  887. out:
  888. if (new_cap)
  889. ceph_put_cap(mdsc, new_cap);
  890. if (xattr_blob)
  891. ceph_buffer_put(xattr_blob);
  892. ceph_put_string(pool_ns);
  893. return err;
  894. }
  895. /*
  896. * caller should hold session s_mutex.
  897. */
  898. static void update_dentry_lease(struct dentry *dentry,
  899. struct ceph_mds_reply_lease *lease,
  900. struct ceph_mds_session *session,
  901. unsigned long from_time,
  902. struct ceph_vino *tgt_vino,
  903. struct ceph_vino *dir_vino)
  904. {
  905. struct ceph_dentry_info *di = ceph_dentry(dentry);
  906. long unsigned duration = le32_to_cpu(lease->duration_ms);
  907. long unsigned ttl = from_time + (duration * HZ) / 1000;
  908. long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
  909. struct inode *dir;
  910. struct ceph_mds_session *old_lease_session = NULL;
  911. /*
  912. * Make sure dentry's inode matches tgt_vino. NULL tgt_vino means that
  913. * we expect a negative dentry.
  914. */
  915. if (!tgt_vino && d_really_is_positive(dentry))
  916. return;
  917. if (tgt_vino && (d_really_is_negative(dentry) ||
  918. !ceph_ino_compare(d_inode(dentry), tgt_vino)))
  919. return;
  920. spin_lock(&dentry->d_lock);
  921. dout("update_dentry_lease %p duration %lu ms ttl %lu\n",
  922. dentry, duration, ttl);
  923. dir = d_inode(dentry->d_parent);
  924. /* make sure parent matches dir_vino */
  925. if (!ceph_ino_compare(dir, dir_vino))
  926. goto out_unlock;
  927. /* only track leases on regular dentries */
  928. if (ceph_snap(dir) != CEPH_NOSNAP)
  929. goto out_unlock;
  930. di->lease_shared_gen = atomic_read(&ceph_inode(dir)->i_shared_gen);
  931. if (duration == 0)
  932. goto out_unlock;
  933. if (di->lease_gen == session->s_cap_gen &&
  934. time_before(ttl, di->time))
  935. goto out_unlock; /* we already have a newer lease. */
  936. if (di->lease_session && di->lease_session != session) {
  937. old_lease_session = di->lease_session;
  938. di->lease_session = NULL;
  939. }
  940. ceph_dentry_lru_touch(dentry);
  941. if (!di->lease_session)
  942. di->lease_session = ceph_get_mds_session(session);
  943. di->lease_gen = session->s_cap_gen;
  944. di->lease_seq = le32_to_cpu(lease->seq);
  945. di->lease_renew_after = half_ttl;
  946. di->lease_renew_from = 0;
  947. di->time = ttl;
  948. out_unlock:
  949. spin_unlock(&dentry->d_lock);
  950. if (old_lease_session)
  951. ceph_put_mds_session(old_lease_session);
  952. }
  953. /*
  954. * splice a dentry to an inode.
  955. * caller must hold directory i_mutex for this to be safe.
  956. */
  957. static struct dentry *splice_dentry(struct dentry *dn, struct inode *in)
  958. {
  959. struct dentry *realdn;
  960. BUG_ON(d_inode(dn));
  961. if (S_ISDIR(in->i_mode)) {
  962. /* If inode is directory, d_splice_alias() below will remove
  963. * 'realdn' from its origin parent. We need to ensure that
  964. * origin parent's readdir cache will not reference 'realdn'
  965. */
  966. realdn = d_find_any_alias(in);
  967. if (realdn) {
  968. struct ceph_dentry_info *di = ceph_dentry(realdn);
  969. spin_lock(&realdn->d_lock);
  970. realdn->d_op->d_prune(realdn);
  971. di->time = jiffies;
  972. di->lease_shared_gen = 0;
  973. di->offset = 0;
  974. spin_unlock(&realdn->d_lock);
  975. dput(realdn);
  976. }
  977. }
  978. /* dn must be unhashed */
  979. if (!d_unhashed(dn))
  980. d_drop(dn);
  981. realdn = d_splice_alias(in, dn);
  982. if (IS_ERR(realdn)) {
  983. pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n",
  984. PTR_ERR(realdn), dn, in, ceph_vinop(in));
  985. dn = realdn; /* note realdn contains the error */
  986. goto out;
  987. } else if (realdn) {
  988. dout("dn %p (%d) spliced with %p (%d) "
  989. "inode %p ino %llx.%llx\n",
  990. dn, d_count(dn),
  991. realdn, d_count(realdn),
  992. d_inode(realdn), ceph_vinop(d_inode(realdn)));
  993. dput(dn);
  994. dn = realdn;
  995. } else {
  996. BUG_ON(!ceph_dentry(dn));
  997. dout("dn %p attached to %p ino %llx.%llx\n",
  998. dn, d_inode(dn), ceph_vinop(d_inode(dn)));
  999. }
  1000. out:
  1001. return dn;
  1002. }
  1003. /*
  1004. * Incorporate results into the local cache. This is either just
  1005. * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
  1006. * after a lookup).
  1007. *
  1008. * A reply may contain
  1009. * a directory inode along with a dentry.
  1010. * and/or a target inode
  1011. *
  1012. * Called with snap_rwsem (read).
  1013. */
  1014. int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req)
  1015. {
  1016. struct ceph_mds_session *session = req->r_session;
  1017. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  1018. struct inode *in = NULL;
  1019. struct ceph_vino tvino, dvino;
  1020. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  1021. int err = 0;
  1022. dout("fill_trace %p is_dentry %d is_target %d\n", req,
  1023. rinfo->head->is_dentry, rinfo->head->is_target);
  1024. if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
  1025. dout("fill_trace reply is empty!\n");
  1026. if (rinfo->head->result == 0 && req->r_parent)
  1027. ceph_invalidate_dir_request(req);
  1028. return 0;
  1029. }
  1030. if (rinfo->head->is_dentry) {
  1031. struct inode *dir = req->r_parent;
  1032. if (dir) {
  1033. err = fill_inode(dir, NULL,
  1034. &rinfo->diri, rinfo->dirfrag,
  1035. session, req->r_request_started, -1,
  1036. &req->r_caps_reservation);
  1037. if (err < 0)
  1038. goto done;
  1039. } else {
  1040. WARN_ON_ONCE(1);
  1041. }
  1042. if (dir && req->r_op == CEPH_MDS_OP_LOOKUPNAME) {
  1043. struct qstr dname;
  1044. struct dentry *dn, *parent;
  1045. BUG_ON(!rinfo->head->is_target);
  1046. BUG_ON(req->r_dentry);
  1047. parent = d_find_any_alias(dir);
  1048. BUG_ON(!parent);
  1049. dname.name = rinfo->dname;
  1050. dname.len = rinfo->dname_len;
  1051. dname.hash = full_name_hash(parent, dname.name, dname.len);
  1052. tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
  1053. tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
  1054. retry_lookup:
  1055. dn = d_lookup(parent, &dname);
  1056. dout("d_lookup on parent=%p name=%.*s got %p\n",
  1057. parent, dname.len, dname.name, dn);
  1058. if (!dn) {
  1059. dn = d_alloc(parent, &dname);
  1060. dout("d_alloc %p '%.*s' = %p\n", parent,
  1061. dname.len, dname.name, dn);
  1062. if (!dn) {
  1063. dput(parent);
  1064. err = -ENOMEM;
  1065. goto done;
  1066. }
  1067. err = 0;
  1068. } else if (d_really_is_positive(dn) &&
  1069. (ceph_ino(d_inode(dn)) != tvino.ino ||
  1070. ceph_snap(d_inode(dn)) != tvino.snap)) {
  1071. dout(" dn %p points to wrong inode %p\n",
  1072. dn, d_inode(dn));
  1073. ceph_dir_clear_ordered(dir);
  1074. d_delete(dn);
  1075. dput(dn);
  1076. goto retry_lookup;
  1077. }
  1078. req->r_dentry = dn;
  1079. dput(parent);
  1080. }
  1081. }
  1082. if (rinfo->head->is_target) {
  1083. tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
  1084. tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
  1085. in = ceph_get_inode(sb, tvino);
  1086. if (IS_ERR(in)) {
  1087. err = PTR_ERR(in);
  1088. goto done;
  1089. }
  1090. req->r_target_inode = in;
  1091. err = fill_inode(in, req->r_locked_page, &rinfo->targeti, NULL,
  1092. session, req->r_request_started,
  1093. (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
  1094. rinfo->head->result == 0) ? req->r_fmode : -1,
  1095. &req->r_caps_reservation);
  1096. if (err < 0) {
  1097. pr_err("fill_inode badness %p %llx.%llx\n",
  1098. in, ceph_vinop(in));
  1099. goto done;
  1100. }
  1101. }
  1102. /*
  1103. * ignore null lease/binding on snapdir ENOENT, or else we
  1104. * will have trouble splicing in the virtual snapdir later
  1105. */
  1106. if (rinfo->head->is_dentry &&
  1107. !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
  1108. test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
  1109. (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
  1110. fsc->mount_options->snapdir_name,
  1111. req->r_dentry->d_name.len))) {
  1112. /*
  1113. * lookup link rename : null -> possibly existing inode
  1114. * mknod symlink mkdir : null -> new inode
  1115. * unlink : linked -> null
  1116. */
  1117. struct inode *dir = req->r_parent;
  1118. struct dentry *dn = req->r_dentry;
  1119. bool have_dir_cap, have_lease;
  1120. BUG_ON(!dn);
  1121. BUG_ON(!dir);
  1122. BUG_ON(d_inode(dn->d_parent) != dir);
  1123. dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
  1124. dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
  1125. BUG_ON(ceph_ino(dir) != dvino.ino);
  1126. BUG_ON(ceph_snap(dir) != dvino.snap);
  1127. /* do we have a lease on the whole dir? */
  1128. have_dir_cap =
  1129. (le32_to_cpu(rinfo->diri.in->cap.caps) &
  1130. CEPH_CAP_FILE_SHARED);
  1131. /* do we have a dn lease? */
  1132. have_lease = have_dir_cap ||
  1133. le32_to_cpu(rinfo->dlease->duration_ms);
  1134. if (!have_lease)
  1135. dout("fill_trace no dentry lease or dir cap\n");
  1136. /* rename? */
  1137. if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
  1138. struct inode *olddir = req->r_old_dentry_dir;
  1139. BUG_ON(!olddir);
  1140. dout(" src %p '%pd' dst %p '%pd'\n",
  1141. req->r_old_dentry,
  1142. req->r_old_dentry,
  1143. dn, dn);
  1144. dout("fill_trace doing d_move %p -> %p\n",
  1145. req->r_old_dentry, dn);
  1146. /* d_move screws up sibling dentries' offsets */
  1147. ceph_dir_clear_ordered(dir);
  1148. ceph_dir_clear_ordered(olddir);
  1149. d_move(req->r_old_dentry, dn);
  1150. dout(" src %p '%pd' dst %p '%pd'\n",
  1151. req->r_old_dentry,
  1152. req->r_old_dentry,
  1153. dn, dn);
  1154. /* ensure target dentry is invalidated, despite
  1155. rehashing bug in vfs_rename_dir */
  1156. ceph_invalidate_dentry_lease(dn);
  1157. dout("dn %p gets new offset %lld\n", req->r_old_dentry,
  1158. ceph_dentry(req->r_old_dentry)->offset);
  1159. dn = req->r_old_dentry; /* use old_dentry */
  1160. }
  1161. /* null dentry? */
  1162. if (!rinfo->head->is_target) {
  1163. dout("fill_trace null dentry\n");
  1164. if (d_really_is_positive(dn)) {
  1165. dout("d_delete %p\n", dn);
  1166. ceph_dir_clear_ordered(dir);
  1167. d_delete(dn);
  1168. } else if (have_lease) {
  1169. if (d_unhashed(dn))
  1170. d_add(dn, NULL);
  1171. update_dentry_lease(dn, rinfo->dlease,
  1172. session,
  1173. req->r_request_started,
  1174. NULL, &dvino);
  1175. }
  1176. goto done;
  1177. }
  1178. /* attach proper inode */
  1179. if (d_really_is_negative(dn)) {
  1180. ceph_dir_clear_ordered(dir);
  1181. ihold(in);
  1182. dn = splice_dentry(dn, in);
  1183. if (IS_ERR(dn)) {
  1184. err = PTR_ERR(dn);
  1185. goto done;
  1186. }
  1187. req->r_dentry = dn; /* may have spliced */
  1188. } else if (d_really_is_positive(dn) && d_inode(dn) != in) {
  1189. dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
  1190. dn, d_inode(dn), ceph_vinop(d_inode(dn)),
  1191. ceph_vinop(in));
  1192. d_invalidate(dn);
  1193. have_lease = false;
  1194. }
  1195. if (have_lease) {
  1196. tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
  1197. tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
  1198. update_dentry_lease(dn, rinfo->dlease, session,
  1199. req->r_request_started,
  1200. &tvino, &dvino);
  1201. }
  1202. dout(" final dn %p\n", dn);
  1203. } else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
  1204. req->r_op == CEPH_MDS_OP_MKSNAP) &&
  1205. !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
  1206. struct dentry *dn = req->r_dentry;
  1207. struct inode *dir = req->r_parent;
  1208. /* fill out a snapdir LOOKUPSNAP dentry */
  1209. BUG_ON(!dn);
  1210. BUG_ON(!dir);
  1211. BUG_ON(ceph_snap(dir) != CEPH_SNAPDIR);
  1212. dout(" linking snapped dir %p to dn %p\n", in, dn);
  1213. ceph_dir_clear_ordered(dir);
  1214. ihold(in);
  1215. dn = splice_dentry(dn, in);
  1216. if (IS_ERR(dn)) {
  1217. err = PTR_ERR(dn);
  1218. goto done;
  1219. }
  1220. req->r_dentry = dn; /* may have spliced */
  1221. } else if (rinfo->head->is_dentry) {
  1222. struct ceph_vino *ptvino = NULL;
  1223. if ((le32_to_cpu(rinfo->diri.in->cap.caps) & CEPH_CAP_FILE_SHARED) ||
  1224. le32_to_cpu(rinfo->dlease->duration_ms)) {
  1225. dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
  1226. dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
  1227. if (rinfo->head->is_target) {
  1228. tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
  1229. tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
  1230. ptvino = &tvino;
  1231. }
  1232. update_dentry_lease(req->r_dentry, rinfo->dlease,
  1233. session, req->r_request_started, ptvino,
  1234. &dvino);
  1235. } else {
  1236. dout("%s: no dentry lease or dir cap\n", __func__);
  1237. }
  1238. }
  1239. done:
  1240. dout("fill_trace done err=%d\n", err);
  1241. return err;
  1242. }
  1243. /*
  1244. * Prepopulate our cache with readdir results, leases, etc.
  1245. */
  1246. static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req,
  1247. struct ceph_mds_session *session)
  1248. {
  1249. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  1250. int i, err = 0;
  1251. for (i = 0; i < rinfo->dir_nr; i++) {
  1252. struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
  1253. struct ceph_vino vino;
  1254. struct inode *in;
  1255. int rc;
  1256. vino.ino = le64_to_cpu(rde->inode.in->ino);
  1257. vino.snap = le64_to_cpu(rde->inode.in->snapid);
  1258. in = ceph_get_inode(req->r_dentry->d_sb, vino);
  1259. if (IS_ERR(in)) {
  1260. err = PTR_ERR(in);
  1261. dout("new_inode badness got %d\n", err);
  1262. continue;
  1263. }
  1264. rc = fill_inode(in, NULL, &rde->inode, NULL, session,
  1265. req->r_request_started, -1,
  1266. &req->r_caps_reservation);
  1267. if (rc < 0) {
  1268. pr_err("fill_inode badness on %p got %d\n", in, rc);
  1269. err = rc;
  1270. }
  1271. iput(in);
  1272. }
  1273. return err;
  1274. }
  1275. void ceph_readdir_cache_release(struct ceph_readdir_cache_control *ctl)
  1276. {
  1277. if (ctl->page) {
  1278. kunmap(ctl->page);
  1279. put_page(ctl->page);
  1280. ctl->page = NULL;
  1281. }
  1282. }
  1283. static int fill_readdir_cache(struct inode *dir, struct dentry *dn,
  1284. struct ceph_readdir_cache_control *ctl,
  1285. struct ceph_mds_request *req)
  1286. {
  1287. struct ceph_inode_info *ci = ceph_inode(dir);
  1288. unsigned nsize = PAGE_SIZE / sizeof(struct dentry*);
  1289. unsigned idx = ctl->index % nsize;
  1290. pgoff_t pgoff = ctl->index / nsize;
  1291. if (!ctl->page || pgoff != page_index(ctl->page)) {
  1292. ceph_readdir_cache_release(ctl);
  1293. if (idx == 0)
  1294. ctl->page = grab_cache_page(&dir->i_data, pgoff);
  1295. else
  1296. ctl->page = find_lock_page(&dir->i_data, pgoff);
  1297. if (!ctl->page) {
  1298. ctl->index = -1;
  1299. return idx == 0 ? -ENOMEM : 0;
  1300. }
  1301. /* reading/filling the cache are serialized by
  1302. * i_mutex, no need to use page lock */
  1303. unlock_page(ctl->page);
  1304. ctl->dentries = kmap(ctl->page);
  1305. if (idx == 0)
  1306. memset(ctl->dentries, 0, PAGE_SIZE);
  1307. }
  1308. if (req->r_dir_release_cnt == atomic64_read(&ci->i_release_count) &&
  1309. req->r_dir_ordered_cnt == atomic64_read(&ci->i_ordered_count)) {
  1310. dout("readdir cache dn %p idx %d\n", dn, ctl->index);
  1311. ctl->dentries[idx] = dn;
  1312. ctl->index++;
  1313. } else {
  1314. dout("disable readdir cache\n");
  1315. ctl->index = -1;
  1316. }
  1317. return 0;
  1318. }
  1319. int ceph_readdir_prepopulate(struct ceph_mds_request *req,
  1320. struct ceph_mds_session *session)
  1321. {
  1322. struct dentry *parent = req->r_dentry;
  1323. struct ceph_inode_info *ci = ceph_inode(d_inode(parent));
  1324. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  1325. struct qstr dname;
  1326. struct dentry *dn;
  1327. struct inode *in;
  1328. int err = 0, skipped = 0, ret, i;
  1329. struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
  1330. u32 frag = le32_to_cpu(rhead->args.readdir.frag);
  1331. u32 last_hash = 0;
  1332. u32 fpos_offset;
  1333. struct ceph_readdir_cache_control cache_ctl = {};
  1334. if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
  1335. return readdir_prepopulate_inodes_only(req, session);
  1336. if (rinfo->hash_order) {
  1337. if (req->r_path2) {
  1338. last_hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash,
  1339. req->r_path2,
  1340. strlen(req->r_path2));
  1341. last_hash = ceph_frag_value(last_hash);
  1342. } else if (rinfo->offset_hash) {
  1343. /* mds understands offset_hash */
  1344. WARN_ON_ONCE(req->r_readdir_offset != 2);
  1345. last_hash = le32_to_cpu(rhead->args.readdir.offset_hash);
  1346. }
  1347. }
  1348. if (rinfo->dir_dir &&
  1349. le32_to_cpu(rinfo->dir_dir->frag) != frag) {
  1350. dout("readdir_prepopulate got new frag %x -> %x\n",
  1351. frag, le32_to_cpu(rinfo->dir_dir->frag));
  1352. frag = le32_to_cpu(rinfo->dir_dir->frag);
  1353. if (!rinfo->hash_order)
  1354. req->r_readdir_offset = 2;
  1355. }
  1356. if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
  1357. dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
  1358. rinfo->dir_nr, parent);
  1359. } else {
  1360. dout("readdir_prepopulate %d items under dn %p\n",
  1361. rinfo->dir_nr, parent);
  1362. if (rinfo->dir_dir)
  1363. ceph_fill_dirfrag(d_inode(parent), rinfo->dir_dir);
  1364. if (ceph_frag_is_leftmost(frag) &&
  1365. req->r_readdir_offset == 2 &&
  1366. !(rinfo->hash_order && last_hash)) {
  1367. /* note dir version at start of readdir so we can
  1368. * tell if any dentries get dropped */
  1369. req->r_dir_release_cnt =
  1370. atomic64_read(&ci->i_release_count);
  1371. req->r_dir_ordered_cnt =
  1372. atomic64_read(&ci->i_ordered_count);
  1373. req->r_readdir_cache_idx = 0;
  1374. }
  1375. }
  1376. cache_ctl.index = req->r_readdir_cache_idx;
  1377. fpos_offset = req->r_readdir_offset;
  1378. /* FIXME: release caps/leases if error occurs */
  1379. for (i = 0; i < rinfo->dir_nr; i++) {
  1380. struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
  1381. struct ceph_vino tvino, dvino;
  1382. dname.name = rde->name;
  1383. dname.len = rde->name_len;
  1384. dname.hash = full_name_hash(parent, dname.name, dname.len);
  1385. tvino.ino = le64_to_cpu(rde->inode.in->ino);
  1386. tvino.snap = le64_to_cpu(rde->inode.in->snapid);
  1387. if (rinfo->hash_order) {
  1388. u32 hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash,
  1389. rde->name, rde->name_len);
  1390. hash = ceph_frag_value(hash);
  1391. if (hash != last_hash)
  1392. fpos_offset = 2;
  1393. last_hash = hash;
  1394. rde->offset = ceph_make_fpos(hash, fpos_offset++, true);
  1395. } else {
  1396. rde->offset = ceph_make_fpos(frag, fpos_offset++, false);
  1397. }
  1398. retry_lookup:
  1399. dn = d_lookup(parent, &dname);
  1400. dout("d_lookup on parent=%p name=%.*s got %p\n",
  1401. parent, dname.len, dname.name, dn);
  1402. if (!dn) {
  1403. dn = d_alloc(parent, &dname);
  1404. dout("d_alloc %p '%.*s' = %p\n", parent,
  1405. dname.len, dname.name, dn);
  1406. if (!dn) {
  1407. dout("d_alloc badness\n");
  1408. err = -ENOMEM;
  1409. goto out;
  1410. }
  1411. } else if (d_really_is_positive(dn) &&
  1412. (ceph_ino(d_inode(dn)) != tvino.ino ||
  1413. ceph_snap(d_inode(dn)) != tvino.snap)) {
  1414. struct ceph_dentry_info *di = ceph_dentry(dn);
  1415. dout(" dn %p points to wrong inode %p\n",
  1416. dn, d_inode(dn));
  1417. spin_lock(&dn->d_lock);
  1418. if (di->offset > 0 &&
  1419. di->lease_shared_gen ==
  1420. atomic_read(&ci->i_shared_gen)) {
  1421. __ceph_dir_clear_ordered(ci);
  1422. di->offset = 0;
  1423. }
  1424. spin_unlock(&dn->d_lock);
  1425. d_delete(dn);
  1426. dput(dn);
  1427. goto retry_lookup;
  1428. }
  1429. /* inode */
  1430. if (d_really_is_positive(dn)) {
  1431. in = d_inode(dn);
  1432. } else {
  1433. in = ceph_get_inode(parent->d_sb, tvino);
  1434. if (IS_ERR(in)) {
  1435. dout("new_inode badness\n");
  1436. d_drop(dn);
  1437. dput(dn);
  1438. err = PTR_ERR(in);
  1439. goto out;
  1440. }
  1441. }
  1442. ret = fill_inode(in, NULL, &rde->inode, NULL, session,
  1443. req->r_request_started, -1,
  1444. &req->r_caps_reservation);
  1445. if (ret < 0) {
  1446. pr_err("fill_inode badness on %p\n", in);
  1447. if (d_really_is_negative(dn))
  1448. iput(in);
  1449. d_drop(dn);
  1450. err = ret;
  1451. goto next_item;
  1452. }
  1453. if (d_really_is_negative(dn)) {
  1454. struct dentry *realdn;
  1455. if (ceph_security_xattr_deadlock(in)) {
  1456. dout(" skip splicing dn %p to inode %p"
  1457. " (security xattr deadlock)\n", dn, in);
  1458. iput(in);
  1459. skipped++;
  1460. goto next_item;
  1461. }
  1462. realdn = splice_dentry(dn, in);
  1463. if (IS_ERR(realdn)) {
  1464. err = PTR_ERR(realdn);
  1465. d_drop(dn);
  1466. dn = NULL;
  1467. goto next_item;
  1468. }
  1469. dn = realdn;
  1470. }
  1471. ceph_dentry(dn)->offset = rde->offset;
  1472. dvino = ceph_vino(d_inode(parent));
  1473. update_dentry_lease(dn, rde->lease, req->r_session,
  1474. req->r_request_started, &tvino, &dvino);
  1475. if (err == 0 && skipped == 0 && cache_ctl.index >= 0) {
  1476. ret = fill_readdir_cache(d_inode(parent), dn,
  1477. &cache_ctl, req);
  1478. if (ret < 0)
  1479. err = ret;
  1480. }
  1481. next_item:
  1482. if (dn)
  1483. dput(dn);
  1484. }
  1485. out:
  1486. if (err == 0 && skipped == 0) {
  1487. set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags);
  1488. req->r_readdir_cache_idx = cache_ctl.index;
  1489. }
  1490. ceph_readdir_cache_release(&cache_ctl);
  1491. dout("readdir_prepopulate done\n");
  1492. return err;
  1493. }
  1494. bool ceph_inode_set_size(struct inode *inode, loff_t size)
  1495. {
  1496. struct ceph_inode_info *ci = ceph_inode(inode);
  1497. bool ret;
  1498. spin_lock(&ci->i_ceph_lock);
  1499. dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
  1500. i_size_write(inode, size);
  1501. inode->i_blocks = calc_inode_blocks(size);
  1502. ret = __ceph_should_report_size(ci);
  1503. spin_unlock(&ci->i_ceph_lock);
  1504. return ret;
  1505. }
  1506. /*
  1507. * Write back inode data in a worker thread. (This can't be done
  1508. * in the message handler context.)
  1509. */
  1510. void ceph_queue_writeback(struct inode *inode)
  1511. {
  1512. ihold(inode);
  1513. if (queue_work(ceph_inode_to_client(inode)->wb_wq,
  1514. &ceph_inode(inode)->i_wb_work)) {
  1515. dout("ceph_queue_writeback %p\n", inode);
  1516. } else {
  1517. dout("ceph_queue_writeback %p failed\n", inode);
  1518. iput(inode);
  1519. }
  1520. }
  1521. static void ceph_writeback_work(struct work_struct *work)
  1522. {
  1523. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1524. i_wb_work);
  1525. struct inode *inode = &ci->vfs_inode;
  1526. dout("writeback %p\n", inode);
  1527. filemap_fdatawrite(&inode->i_data);
  1528. iput(inode);
  1529. }
  1530. /*
  1531. * queue an async invalidation
  1532. */
  1533. void ceph_queue_invalidate(struct inode *inode)
  1534. {
  1535. ihold(inode);
  1536. if (queue_work(ceph_inode_to_client(inode)->pg_inv_wq,
  1537. &ceph_inode(inode)->i_pg_inv_work)) {
  1538. dout("ceph_queue_invalidate %p\n", inode);
  1539. } else {
  1540. dout("ceph_queue_invalidate %p failed\n", inode);
  1541. iput(inode);
  1542. }
  1543. }
  1544. /*
  1545. * Invalidate inode pages in a worker thread. (This can't be done
  1546. * in the message handler context.)
  1547. */
  1548. static void ceph_invalidate_work(struct work_struct *work)
  1549. {
  1550. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1551. i_pg_inv_work);
  1552. struct inode *inode = &ci->vfs_inode;
  1553. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  1554. u32 orig_gen;
  1555. int check = 0;
  1556. mutex_lock(&ci->i_truncate_mutex);
  1557. if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
  1558. pr_warn_ratelimited("invalidate_pages %p %lld forced umount\n",
  1559. inode, ceph_ino(inode));
  1560. mapping_set_error(inode->i_mapping, -EIO);
  1561. truncate_pagecache(inode, 0);
  1562. mutex_unlock(&ci->i_truncate_mutex);
  1563. goto out;
  1564. }
  1565. spin_lock(&ci->i_ceph_lock);
  1566. dout("invalidate_pages %p gen %d revoking %d\n", inode,
  1567. ci->i_rdcache_gen, ci->i_rdcache_revoking);
  1568. if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
  1569. if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
  1570. check = 1;
  1571. spin_unlock(&ci->i_ceph_lock);
  1572. mutex_unlock(&ci->i_truncate_mutex);
  1573. goto out;
  1574. }
  1575. orig_gen = ci->i_rdcache_gen;
  1576. spin_unlock(&ci->i_ceph_lock);
  1577. if (invalidate_inode_pages2(inode->i_mapping) < 0) {
  1578. pr_err("invalidate_pages %p fails\n", inode);
  1579. }
  1580. spin_lock(&ci->i_ceph_lock);
  1581. if (orig_gen == ci->i_rdcache_gen &&
  1582. orig_gen == ci->i_rdcache_revoking) {
  1583. dout("invalidate_pages %p gen %d successful\n", inode,
  1584. ci->i_rdcache_gen);
  1585. ci->i_rdcache_revoking--;
  1586. check = 1;
  1587. } else {
  1588. dout("invalidate_pages %p gen %d raced, now %d revoking %d\n",
  1589. inode, orig_gen, ci->i_rdcache_gen,
  1590. ci->i_rdcache_revoking);
  1591. if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
  1592. check = 1;
  1593. }
  1594. spin_unlock(&ci->i_ceph_lock);
  1595. mutex_unlock(&ci->i_truncate_mutex);
  1596. out:
  1597. if (check)
  1598. ceph_check_caps(ci, 0, NULL);
  1599. iput(inode);
  1600. }
  1601. /*
  1602. * called by trunc_wq;
  1603. *
  1604. * We also truncate in a separate thread as well.
  1605. */
  1606. static void ceph_vmtruncate_work(struct work_struct *work)
  1607. {
  1608. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1609. i_vmtruncate_work);
  1610. struct inode *inode = &ci->vfs_inode;
  1611. dout("vmtruncate_work %p\n", inode);
  1612. __ceph_do_pending_vmtruncate(inode);
  1613. iput(inode);
  1614. }
  1615. /*
  1616. * Queue an async vmtruncate. If we fail to queue work, we will handle
  1617. * the truncation the next time we call __ceph_do_pending_vmtruncate.
  1618. */
  1619. void ceph_queue_vmtruncate(struct inode *inode)
  1620. {
  1621. struct ceph_inode_info *ci = ceph_inode(inode);
  1622. ihold(inode);
  1623. if (queue_work(ceph_sb_to_client(inode->i_sb)->trunc_wq,
  1624. &ci->i_vmtruncate_work)) {
  1625. dout("ceph_queue_vmtruncate %p\n", inode);
  1626. } else {
  1627. dout("ceph_queue_vmtruncate %p failed, pending=%d\n",
  1628. inode, ci->i_truncate_pending);
  1629. iput(inode);
  1630. }
  1631. }
  1632. /*
  1633. * Make sure any pending truncation is applied before doing anything
  1634. * that may depend on it.
  1635. */
  1636. void __ceph_do_pending_vmtruncate(struct inode *inode)
  1637. {
  1638. struct ceph_inode_info *ci = ceph_inode(inode);
  1639. u64 to;
  1640. int wrbuffer_refs, finish = 0;
  1641. mutex_lock(&ci->i_truncate_mutex);
  1642. retry:
  1643. spin_lock(&ci->i_ceph_lock);
  1644. if (ci->i_truncate_pending == 0) {
  1645. dout("__do_pending_vmtruncate %p none pending\n", inode);
  1646. spin_unlock(&ci->i_ceph_lock);
  1647. mutex_unlock(&ci->i_truncate_mutex);
  1648. return;
  1649. }
  1650. /*
  1651. * make sure any dirty snapped pages are flushed before we
  1652. * possibly truncate them.. so write AND block!
  1653. */
  1654. if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
  1655. spin_unlock(&ci->i_ceph_lock);
  1656. dout("__do_pending_vmtruncate %p flushing snaps first\n",
  1657. inode);
  1658. filemap_write_and_wait_range(&inode->i_data, 0,
  1659. inode->i_sb->s_maxbytes);
  1660. goto retry;
  1661. }
  1662. /* there should be no reader or writer */
  1663. WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref);
  1664. to = ci->i_truncate_size;
  1665. wrbuffer_refs = ci->i_wrbuffer_ref;
  1666. dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
  1667. ci->i_truncate_pending, to);
  1668. spin_unlock(&ci->i_ceph_lock);
  1669. truncate_pagecache(inode, to);
  1670. spin_lock(&ci->i_ceph_lock);
  1671. if (to == ci->i_truncate_size) {
  1672. ci->i_truncate_pending = 0;
  1673. finish = 1;
  1674. }
  1675. spin_unlock(&ci->i_ceph_lock);
  1676. if (!finish)
  1677. goto retry;
  1678. mutex_unlock(&ci->i_truncate_mutex);
  1679. if (wrbuffer_refs == 0)
  1680. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  1681. wake_up_all(&ci->i_cap_wq);
  1682. }
  1683. /*
  1684. * symlinks
  1685. */
  1686. static const struct inode_operations ceph_symlink_iops = {
  1687. .get_link = simple_get_link,
  1688. .setattr = ceph_setattr,
  1689. .getattr = ceph_getattr,
  1690. .listxattr = ceph_listxattr,
  1691. };
  1692. int __ceph_setattr(struct inode *inode, struct iattr *attr)
  1693. {
  1694. struct ceph_inode_info *ci = ceph_inode(inode);
  1695. const unsigned int ia_valid = attr->ia_valid;
  1696. struct ceph_mds_request *req;
  1697. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  1698. struct ceph_cap_flush *prealloc_cf;
  1699. int issued;
  1700. int release = 0, dirtied = 0;
  1701. int mask = 0;
  1702. int err = 0;
  1703. int inode_dirty_flags = 0;
  1704. bool lock_snap_rwsem = false;
  1705. struct timespec ts;
  1706. prealloc_cf = ceph_alloc_cap_flush();
  1707. if (!prealloc_cf)
  1708. return -ENOMEM;
  1709. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
  1710. USE_AUTH_MDS);
  1711. if (IS_ERR(req)) {
  1712. ceph_free_cap_flush(prealloc_cf);
  1713. return PTR_ERR(req);
  1714. }
  1715. spin_lock(&ci->i_ceph_lock);
  1716. issued = __ceph_caps_issued(ci, NULL);
  1717. if (!ci->i_head_snapc &&
  1718. (issued & (CEPH_CAP_ANY_EXCL | CEPH_CAP_FILE_WR))) {
  1719. lock_snap_rwsem = true;
  1720. if (!down_read_trylock(&mdsc->snap_rwsem)) {
  1721. spin_unlock(&ci->i_ceph_lock);
  1722. down_read(&mdsc->snap_rwsem);
  1723. spin_lock(&ci->i_ceph_lock);
  1724. issued = __ceph_caps_issued(ci, NULL);
  1725. }
  1726. }
  1727. dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
  1728. if (ia_valid & ATTR_UID) {
  1729. dout("setattr %p uid %d -> %d\n", inode,
  1730. from_kuid(&init_user_ns, inode->i_uid),
  1731. from_kuid(&init_user_ns, attr->ia_uid));
  1732. if (issued & CEPH_CAP_AUTH_EXCL) {
  1733. inode->i_uid = attr->ia_uid;
  1734. dirtied |= CEPH_CAP_AUTH_EXCL;
  1735. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1736. !uid_eq(attr->ia_uid, inode->i_uid)) {
  1737. req->r_args.setattr.uid = cpu_to_le32(
  1738. from_kuid(&init_user_ns, attr->ia_uid));
  1739. mask |= CEPH_SETATTR_UID;
  1740. release |= CEPH_CAP_AUTH_SHARED;
  1741. }
  1742. }
  1743. if (ia_valid & ATTR_GID) {
  1744. dout("setattr %p gid %d -> %d\n", inode,
  1745. from_kgid(&init_user_ns, inode->i_gid),
  1746. from_kgid(&init_user_ns, attr->ia_gid));
  1747. if (issued & CEPH_CAP_AUTH_EXCL) {
  1748. inode->i_gid = attr->ia_gid;
  1749. dirtied |= CEPH_CAP_AUTH_EXCL;
  1750. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1751. !gid_eq(attr->ia_gid, inode->i_gid)) {
  1752. req->r_args.setattr.gid = cpu_to_le32(
  1753. from_kgid(&init_user_ns, attr->ia_gid));
  1754. mask |= CEPH_SETATTR_GID;
  1755. release |= CEPH_CAP_AUTH_SHARED;
  1756. }
  1757. }
  1758. if (ia_valid & ATTR_MODE) {
  1759. dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
  1760. attr->ia_mode);
  1761. if (issued & CEPH_CAP_AUTH_EXCL) {
  1762. inode->i_mode = attr->ia_mode;
  1763. dirtied |= CEPH_CAP_AUTH_EXCL;
  1764. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1765. attr->ia_mode != inode->i_mode) {
  1766. inode->i_mode = attr->ia_mode;
  1767. req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
  1768. mask |= CEPH_SETATTR_MODE;
  1769. release |= CEPH_CAP_AUTH_SHARED;
  1770. }
  1771. }
  1772. if (ia_valid & ATTR_ATIME) {
  1773. dout("setattr %p atime %lld.%ld -> %lld.%ld\n", inode,
  1774. (long long)inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
  1775. (long long)attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
  1776. if (issued & CEPH_CAP_FILE_EXCL) {
  1777. ci->i_time_warp_seq++;
  1778. inode->i_atime = attr->ia_atime;
  1779. dirtied |= CEPH_CAP_FILE_EXCL;
  1780. } else if ((issued & CEPH_CAP_FILE_WR) &&
  1781. timespec64_compare(&inode->i_atime,
  1782. &attr->ia_atime) < 0) {
  1783. inode->i_atime = attr->ia_atime;
  1784. dirtied |= CEPH_CAP_FILE_WR;
  1785. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1786. !timespec64_equal(&inode->i_atime, &attr->ia_atime)) {
  1787. ts = timespec64_to_timespec(attr->ia_atime);
  1788. ceph_encode_timespec(&req->r_args.setattr.atime, &ts);
  1789. mask |= CEPH_SETATTR_ATIME;
  1790. release |= CEPH_CAP_FILE_SHARED |
  1791. CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
  1792. }
  1793. }
  1794. if (ia_valid & ATTR_MTIME) {
  1795. dout("setattr %p mtime %lld.%ld -> %lld.%ld\n", inode,
  1796. (long long)inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
  1797. (long long)attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
  1798. if (issued & CEPH_CAP_FILE_EXCL) {
  1799. ci->i_time_warp_seq++;
  1800. inode->i_mtime = attr->ia_mtime;
  1801. dirtied |= CEPH_CAP_FILE_EXCL;
  1802. } else if ((issued & CEPH_CAP_FILE_WR) &&
  1803. timespec64_compare(&inode->i_mtime,
  1804. &attr->ia_mtime) < 0) {
  1805. inode->i_mtime = attr->ia_mtime;
  1806. dirtied |= CEPH_CAP_FILE_WR;
  1807. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1808. !timespec64_equal(&inode->i_mtime, &attr->ia_mtime)) {
  1809. ts = timespec64_to_timespec(attr->ia_mtime);
  1810. ceph_encode_timespec(&req->r_args.setattr.mtime, &ts);
  1811. mask |= CEPH_SETATTR_MTIME;
  1812. release |= CEPH_CAP_FILE_SHARED |
  1813. CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
  1814. }
  1815. }
  1816. if (ia_valid & ATTR_SIZE) {
  1817. dout("setattr %p size %lld -> %lld\n", inode,
  1818. inode->i_size, attr->ia_size);
  1819. if ((issued & CEPH_CAP_FILE_EXCL) &&
  1820. attr->ia_size > inode->i_size) {
  1821. i_size_write(inode, attr->ia_size);
  1822. inode->i_blocks = calc_inode_blocks(attr->ia_size);
  1823. ci->i_reported_size = attr->ia_size;
  1824. dirtied |= CEPH_CAP_FILE_EXCL;
  1825. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1826. attr->ia_size != inode->i_size) {
  1827. req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
  1828. req->r_args.setattr.old_size =
  1829. cpu_to_le64(inode->i_size);
  1830. mask |= CEPH_SETATTR_SIZE;
  1831. release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL |
  1832. CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
  1833. }
  1834. }
  1835. /* these do nothing */
  1836. if (ia_valid & ATTR_CTIME) {
  1837. bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
  1838. ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
  1839. dout("setattr %p ctime %lld.%ld -> %lld.%ld (%s)\n", inode,
  1840. (long long)inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
  1841. (long long)attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
  1842. only ? "ctime only" : "ignored");
  1843. if (only) {
  1844. /*
  1845. * if kernel wants to dirty ctime but nothing else,
  1846. * we need to choose a cap to dirty under, or do
  1847. * a almost-no-op setattr
  1848. */
  1849. if (issued & CEPH_CAP_AUTH_EXCL)
  1850. dirtied |= CEPH_CAP_AUTH_EXCL;
  1851. else if (issued & CEPH_CAP_FILE_EXCL)
  1852. dirtied |= CEPH_CAP_FILE_EXCL;
  1853. else if (issued & CEPH_CAP_XATTR_EXCL)
  1854. dirtied |= CEPH_CAP_XATTR_EXCL;
  1855. else
  1856. mask |= CEPH_SETATTR_CTIME;
  1857. }
  1858. }
  1859. if (ia_valid & ATTR_FILE)
  1860. dout("setattr %p ATTR_FILE ... hrm!\n", inode);
  1861. if (dirtied) {
  1862. inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied,
  1863. &prealloc_cf);
  1864. inode->i_ctime = attr->ia_ctime;
  1865. }
  1866. release &= issued;
  1867. spin_unlock(&ci->i_ceph_lock);
  1868. if (lock_snap_rwsem)
  1869. up_read(&mdsc->snap_rwsem);
  1870. if (inode_dirty_flags)
  1871. __mark_inode_dirty(inode, inode_dirty_flags);
  1872. if (mask) {
  1873. req->r_inode = inode;
  1874. ihold(inode);
  1875. req->r_inode_drop = release;
  1876. req->r_args.setattr.mask = cpu_to_le32(mask);
  1877. req->r_num_caps = 1;
  1878. req->r_stamp = timespec64_to_timespec(attr->ia_ctime);
  1879. err = ceph_mdsc_do_request(mdsc, NULL, req);
  1880. }
  1881. dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
  1882. ceph_cap_string(dirtied), mask);
  1883. ceph_mdsc_put_request(req);
  1884. ceph_free_cap_flush(prealloc_cf);
  1885. if (err >= 0 && (mask & CEPH_SETATTR_SIZE))
  1886. __ceph_do_pending_vmtruncate(inode);
  1887. return err;
  1888. }
  1889. /*
  1890. * setattr
  1891. */
  1892. int ceph_setattr(struct dentry *dentry, struct iattr *attr)
  1893. {
  1894. struct inode *inode = d_inode(dentry);
  1895. int err;
  1896. if (ceph_snap(inode) != CEPH_NOSNAP)
  1897. return -EROFS;
  1898. err = setattr_prepare(dentry, attr);
  1899. if (err != 0)
  1900. return err;
  1901. if ((attr->ia_valid & ATTR_SIZE) &&
  1902. ceph_quota_is_max_bytes_exceeded(inode, attr->ia_size))
  1903. return -EDQUOT;
  1904. err = __ceph_setattr(inode, attr);
  1905. if (err >= 0 && (attr->ia_valid & ATTR_MODE))
  1906. err = posix_acl_chmod(inode, attr->ia_mode);
  1907. return err;
  1908. }
  1909. /*
  1910. * Verify that we have a lease on the given mask. If not,
  1911. * do a getattr against an mds.
  1912. */
  1913. int __ceph_do_getattr(struct inode *inode, struct page *locked_page,
  1914. int mask, bool force)
  1915. {
  1916. struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
  1917. struct ceph_mds_client *mdsc = fsc->mdsc;
  1918. struct ceph_mds_request *req;
  1919. int err;
  1920. if (ceph_snap(inode) == CEPH_SNAPDIR) {
  1921. dout("do_getattr inode %p SNAPDIR\n", inode);
  1922. return 0;
  1923. }
  1924. dout("do_getattr inode %p mask %s mode 0%o\n",
  1925. inode, ceph_cap_string(mask), inode->i_mode);
  1926. if (!force && ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
  1927. return 0;
  1928. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
  1929. if (IS_ERR(req))
  1930. return PTR_ERR(req);
  1931. req->r_inode = inode;
  1932. ihold(inode);
  1933. req->r_num_caps = 1;
  1934. req->r_args.getattr.mask = cpu_to_le32(mask);
  1935. req->r_locked_page = locked_page;
  1936. err = ceph_mdsc_do_request(mdsc, NULL, req);
  1937. if (locked_page && err == 0) {
  1938. u64 inline_version = req->r_reply_info.targeti.inline_version;
  1939. if (inline_version == 0) {
  1940. /* the reply is supposed to contain inline data */
  1941. err = -EINVAL;
  1942. } else if (inline_version == CEPH_INLINE_NONE) {
  1943. err = -ENODATA;
  1944. } else {
  1945. err = req->r_reply_info.targeti.inline_len;
  1946. }
  1947. }
  1948. ceph_mdsc_put_request(req);
  1949. dout("do_getattr result=%d\n", err);
  1950. return err;
  1951. }
  1952. /*
  1953. * Check inode permissions. We verify we have a valid value for
  1954. * the AUTH cap, then call the generic handler.
  1955. */
  1956. int ceph_permission(struct inode *inode, int mask)
  1957. {
  1958. int err;
  1959. if (mask & MAY_NOT_BLOCK)
  1960. return -ECHILD;
  1961. err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, false);
  1962. if (!err)
  1963. err = generic_permission(inode, mask);
  1964. return err;
  1965. }
  1966. /*
  1967. * Get all attributes. Hopefully somedata we'll have a statlite()
  1968. * and can limit the fields we require to be accurate.
  1969. */
  1970. int ceph_getattr(const struct path *path, struct kstat *stat,
  1971. u32 request_mask, unsigned int flags)
  1972. {
  1973. struct inode *inode = d_inode(path->dentry);
  1974. struct ceph_inode_info *ci = ceph_inode(inode);
  1975. int err;
  1976. err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL, false);
  1977. if (!err) {
  1978. generic_fillattr(inode, stat);
  1979. stat->ino = ceph_translate_ino(inode->i_sb, inode->i_ino);
  1980. if (ceph_snap(inode) != CEPH_NOSNAP)
  1981. stat->dev = ceph_snap(inode);
  1982. else
  1983. stat->dev = 0;
  1984. if (S_ISDIR(inode->i_mode)) {
  1985. if (ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb),
  1986. RBYTES))
  1987. stat->size = ci->i_rbytes;
  1988. else
  1989. stat->size = ci->i_files + ci->i_subdirs;
  1990. stat->blocks = 0;
  1991. stat->blksize = 65536;
  1992. }
  1993. }
  1994. return err;
  1995. }