inode.c 50 KB

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