inode.c 51 KB

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