inode.c 53 KB

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