inode.c 51 KB

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