inode.c 59 KB

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