inode.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918
  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_inode_frag *frag;
  509. struct rb_node *rb_node;
  510. struct ceph_buffer *xattr_blob = NULL;
  511. int err = 0;
  512. int queue_trunc = 0;
  513. dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
  514. inode, ceph_vinop(inode), le64_to_cpu(info->version),
  515. ci->i_version);
  516. /*
  517. * prealloc xattr data, if it looks like we'll need it. only
  518. * if len > 4 (meaning there are actually xattrs; the first 4
  519. * bytes are the xattr count).
  520. */
  521. if (iinfo->xattr_len > 4) {
  522. xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
  523. if (!xattr_blob)
  524. pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
  525. iinfo->xattr_len);
  526. }
  527. spin_lock(&ci->i_ceph_lock);
  528. /*
  529. * provided version will be odd if inode value is projected,
  530. * even if stable. skip the update if we have newer stable
  531. * info (ours>=theirs, e.g. due to racing mds replies), unless
  532. * we are getting projected (unstable) info (in which case the
  533. * version is odd, and we want ours>theirs).
  534. * us them
  535. * 2 2 skip
  536. * 3 2 skip
  537. * 3 3 update
  538. */
  539. if (le64_to_cpu(info->version) > 0 &&
  540. (ci->i_version & ~1) >= le64_to_cpu(info->version))
  541. goto no_change;
  542. issued = __ceph_caps_issued(ci, &implemented);
  543. issued |= implemented | __ceph_caps_dirty(ci);
  544. /* update inode */
  545. ci->i_version = le64_to_cpu(info->version);
  546. inode->i_version++;
  547. inode->i_rdev = le32_to_cpu(info->rdev);
  548. if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
  549. inode->i_mode = le32_to_cpu(info->mode);
  550. inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid));
  551. inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid));
  552. dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
  553. from_kuid(&init_user_ns, inode->i_uid),
  554. from_kgid(&init_user_ns, inode->i_gid));
  555. }
  556. if ((issued & CEPH_CAP_LINK_EXCL) == 0)
  557. set_nlink(inode, le32_to_cpu(info->nlink));
  558. /* be careful with mtime, atime, size */
  559. ceph_decode_timespec(&atime, &info->atime);
  560. ceph_decode_timespec(&mtime, &info->mtime);
  561. ceph_decode_timespec(&ctime, &info->ctime);
  562. queue_trunc = ceph_fill_file_size(inode, issued,
  563. le32_to_cpu(info->truncate_seq),
  564. le64_to_cpu(info->truncate_size),
  565. le64_to_cpu(info->size));
  566. ceph_fill_file_time(inode, issued,
  567. le32_to_cpu(info->time_warp_seq),
  568. &ctime, &mtime, &atime);
  569. /* only update max_size on auth cap */
  570. if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
  571. ci->i_max_size != le64_to_cpu(info->max_size)) {
  572. dout("max_size %lld -> %llu\n", ci->i_max_size,
  573. le64_to_cpu(info->max_size));
  574. ci->i_max_size = le64_to_cpu(info->max_size);
  575. }
  576. ci->i_layout = info->layout;
  577. inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
  578. /* xattrs */
  579. /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
  580. if ((issued & CEPH_CAP_XATTR_EXCL) == 0 &&
  581. le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
  582. if (ci->i_xattrs.blob)
  583. ceph_buffer_put(ci->i_xattrs.blob);
  584. ci->i_xattrs.blob = xattr_blob;
  585. if (xattr_blob)
  586. memcpy(ci->i_xattrs.blob->vec.iov_base,
  587. iinfo->xattr_data, iinfo->xattr_len);
  588. ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
  589. xattr_blob = NULL;
  590. }
  591. inode->i_mapping->a_ops = &ceph_aops;
  592. inode->i_mapping->backing_dev_info =
  593. &ceph_sb_to_client(inode->i_sb)->backing_dev_info;
  594. switch (inode->i_mode & S_IFMT) {
  595. case S_IFIFO:
  596. case S_IFBLK:
  597. case S_IFCHR:
  598. case S_IFSOCK:
  599. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  600. inode->i_op = &ceph_file_iops;
  601. break;
  602. case S_IFREG:
  603. inode->i_op = &ceph_file_iops;
  604. inode->i_fop = &ceph_file_fops;
  605. break;
  606. case S_IFLNK:
  607. inode->i_op = &ceph_symlink_iops;
  608. if (!ci->i_symlink) {
  609. u32 symlen = iinfo->symlink_len;
  610. char *sym;
  611. spin_unlock(&ci->i_ceph_lock);
  612. err = -EINVAL;
  613. if (WARN_ON(symlen != inode->i_size))
  614. goto out;
  615. err = -ENOMEM;
  616. sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS);
  617. if (!sym)
  618. goto out;
  619. spin_lock(&ci->i_ceph_lock);
  620. if (!ci->i_symlink)
  621. ci->i_symlink = sym;
  622. else
  623. kfree(sym); /* lost a race */
  624. }
  625. break;
  626. case S_IFDIR:
  627. inode->i_op = &ceph_dir_iops;
  628. inode->i_fop = &ceph_dir_fops;
  629. ci->i_dir_layout = iinfo->dir_layout;
  630. ci->i_files = le64_to_cpu(info->files);
  631. ci->i_subdirs = le64_to_cpu(info->subdirs);
  632. ci->i_rbytes = le64_to_cpu(info->rbytes);
  633. ci->i_rfiles = le64_to_cpu(info->rfiles);
  634. ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
  635. ceph_decode_timespec(&ci->i_rctime, &info->rctime);
  636. break;
  637. default:
  638. pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
  639. ceph_vinop(inode), inode->i_mode);
  640. }
  641. /* set dir completion flag? */
  642. if (S_ISDIR(inode->i_mode) &&
  643. ci->i_files == 0 && ci->i_subdirs == 0 &&
  644. ceph_snap(inode) == CEPH_NOSNAP &&
  645. (le32_to_cpu(info->cap.caps) & CEPH_CAP_FILE_SHARED) &&
  646. (issued & CEPH_CAP_FILE_EXCL) == 0 &&
  647. !__ceph_dir_is_complete(ci)) {
  648. dout(" marking %p complete (empty)\n", inode);
  649. __ceph_dir_set_complete(ci, atomic_read(&ci->i_release_count));
  650. ci->i_max_offset = 2;
  651. }
  652. no_change:
  653. spin_unlock(&ci->i_ceph_lock);
  654. /* queue truncate if we saw i_size decrease */
  655. if (queue_trunc)
  656. ceph_queue_vmtruncate(inode);
  657. /* populate frag tree */
  658. /* FIXME: move me up, if/when version reflects fragtree changes */
  659. nsplits = le32_to_cpu(info->fragtree.nsplits);
  660. mutex_lock(&ci->i_fragtree_mutex);
  661. rb_node = rb_first(&ci->i_fragtree);
  662. for (i = 0; i < nsplits; i++) {
  663. u32 id = le32_to_cpu(info->fragtree.splits[i].frag);
  664. frag = NULL;
  665. while (rb_node) {
  666. frag = rb_entry(rb_node, struct ceph_inode_frag, node);
  667. if (ceph_frag_compare(frag->frag, id) >= 0) {
  668. if (frag->frag != id)
  669. frag = NULL;
  670. else
  671. rb_node = rb_next(rb_node);
  672. break;
  673. }
  674. rb_node = rb_next(rb_node);
  675. rb_erase(&frag->node, &ci->i_fragtree);
  676. kfree(frag);
  677. frag = NULL;
  678. }
  679. if (!frag) {
  680. frag = __get_or_create_frag(ci, id);
  681. if (IS_ERR(frag))
  682. continue;
  683. }
  684. frag->split_by = le32_to_cpu(info->fragtree.splits[i].by);
  685. dout(" frag %x split by %d\n", frag->frag, frag->split_by);
  686. }
  687. while (rb_node) {
  688. frag = rb_entry(rb_node, struct ceph_inode_frag, node);
  689. rb_node = rb_next(rb_node);
  690. rb_erase(&frag->node, &ci->i_fragtree);
  691. kfree(frag);
  692. }
  693. mutex_unlock(&ci->i_fragtree_mutex);
  694. /* were we issued a capability? */
  695. if (info->cap.caps) {
  696. if (ceph_snap(inode) == CEPH_NOSNAP) {
  697. ceph_add_cap(inode, session,
  698. le64_to_cpu(info->cap.cap_id),
  699. cap_fmode,
  700. le32_to_cpu(info->cap.caps),
  701. le32_to_cpu(info->cap.wanted),
  702. le32_to_cpu(info->cap.seq),
  703. le32_to_cpu(info->cap.mseq),
  704. le64_to_cpu(info->cap.realm),
  705. info->cap.flags,
  706. caps_reservation);
  707. } else {
  708. spin_lock(&ci->i_ceph_lock);
  709. dout(" %p got snap_caps %s\n", inode,
  710. ceph_cap_string(le32_to_cpu(info->cap.caps)));
  711. ci->i_snap_caps |= le32_to_cpu(info->cap.caps);
  712. if (cap_fmode >= 0)
  713. __ceph_get_fmode(ci, cap_fmode);
  714. spin_unlock(&ci->i_ceph_lock);
  715. }
  716. } else if (cap_fmode >= 0) {
  717. pr_warning("mds issued no caps on %llx.%llx\n",
  718. ceph_vinop(inode));
  719. __ceph_get_fmode(ci, cap_fmode);
  720. }
  721. /* update delegation info? */
  722. if (dirinfo)
  723. ceph_fill_dirfrag(inode, dirinfo);
  724. err = 0;
  725. out:
  726. if (xattr_blob)
  727. ceph_buffer_put(xattr_blob);
  728. return err;
  729. }
  730. /*
  731. * caller should hold session s_mutex.
  732. */
  733. static void update_dentry_lease(struct dentry *dentry,
  734. struct ceph_mds_reply_lease *lease,
  735. struct ceph_mds_session *session,
  736. unsigned long from_time)
  737. {
  738. struct ceph_dentry_info *di = ceph_dentry(dentry);
  739. long unsigned duration = le32_to_cpu(lease->duration_ms);
  740. long unsigned ttl = from_time + (duration * HZ) / 1000;
  741. long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
  742. struct inode *dir;
  743. /* only track leases on regular dentries */
  744. if (dentry->d_op != &ceph_dentry_ops)
  745. return;
  746. spin_lock(&dentry->d_lock);
  747. dout("update_dentry_lease %p duration %lu ms ttl %lu\n",
  748. dentry, duration, ttl);
  749. /* make lease_rdcache_gen match directory */
  750. dir = dentry->d_parent->d_inode;
  751. di->lease_shared_gen = ceph_inode(dir)->i_shared_gen;
  752. if (duration == 0)
  753. goto out_unlock;
  754. if (di->lease_gen == session->s_cap_gen &&
  755. time_before(ttl, dentry->d_time))
  756. goto out_unlock; /* we already have a newer lease. */
  757. if (di->lease_session && di->lease_session != session)
  758. goto out_unlock;
  759. ceph_dentry_lru_touch(dentry);
  760. if (!di->lease_session)
  761. di->lease_session = ceph_get_mds_session(session);
  762. di->lease_gen = session->s_cap_gen;
  763. di->lease_seq = le32_to_cpu(lease->seq);
  764. di->lease_renew_after = half_ttl;
  765. di->lease_renew_from = 0;
  766. dentry->d_time = ttl;
  767. out_unlock:
  768. spin_unlock(&dentry->d_lock);
  769. return;
  770. }
  771. /*
  772. * Set dentry's directory position based on the current dir's max, and
  773. * order it in d_subdirs, so that dcache_readdir behaves.
  774. *
  775. * Always called under directory's i_mutex.
  776. */
  777. static void ceph_set_dentry_offset(struct dentry *dn)
  778. {
  779. struct dentry *dir = dn->d_parent;
  780. struct inode *inode = dir->d_inode;
  781. struct ceph_inode_info *ci;
  782. struct ceph_dentry_info *di;
  783. BUG_ON(!inode);
  784. ci = ceph_inode(inode);
  785. di = ceph_dentry(dn);
  786. spin_lock(&ci->i_ceph_lock);
  787. if (!__ceph_dir_is_complete(ci)) {
  788. spin_unlock(&ci->i_ceph_lock);
  789. return;
  790. }
  791. di->offset = ceph_inode(inode)->i_max_offset++;
  792. spin_unlock(&ci->i_ceph_lock);
  793. spin_lock(&dir->d_lock);
  794. spin_lock_nested(&dn->d_lock, DENTRY_D_LOCK_NESTED);
  795. list_move(&dn->d_u.d_child, &dir->d_subdirs);
  796. dout("set_dentry_offset %p %lld (%p %p)\n", dn, di->offset,
  797. dn->d_u.d_child.prev, dn->d_u.d_child.next);
  798. spin_unlock(&dn->d_lock);
  799. spin_unlock(&dir->d_lock);
  800. }
  801. /*
  802. * splice a dentry to an inode.
  803. * caller must hold directory i_mutex for this to be safe.
  804. *
  805. * we will only rehash the resulting dentry if @prehash is
  806. * true; @prehash will be set to false (for the benefit of
  807. * the caller) if we fail.
  808. */
  809. static struct dentry *splice_dentry(struct dentry *dn, struct inode *in,
  810. bool *prehash, bool set_offset)
  811. {
  812. struct dentry *realdn;
  813. BUG_ON(dn->d_inode);
  814. /* dn must be unhashed */
  815. if (!d_unhashed(dn))
  816. d_drop(dn);
  817. realdn = d_materialise_unique(dn, in);
  818. if (IS_ERR(realdn)) {
  819. pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n",
  820. PTR_ERR(realdn), dn, in, ceph_vinop(in));
  821. if (prehash)
  822. *prehash = false; /* don't rehash on error */
  823. dn = realdn; /* note realdn contains the error */
  824. goto out;
  825. } else if (realdn) {
  826. dout("dn %p (%d) spliced with %p (%d) "
  827. "inode %p ino %llx.%llx\n",
  828. dn, d_count(dn),
  829. realdn, d_count(realdn),
  830. realdn->d_inode, ceph_vinop(realdn->d_inode));
  831. dput(dn);
  832. dn = realdn;
  833. } else {
  834. BUG_ON(!ceph_dentry(dn));
  835. dout("dn %p attached to %p ino %llx.%llx\n",
  836. dn, dn->d_inode, ceph_vinop(dn->d_inode));
  837. }
  838. if ((!prehash || *prehash) && d_unhashed(dn))
  839. d_rehash(dn);
  840. if (set_offset)
  841. ceph_set_dentry_offset(dn);
  842. out:
  843. return dn;
  844. }
  845. /*
  846. * Incorporate results into the local cache. This is either just
  847. * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
  848. * after a lookup).
  849. *
  850. * A reply may contain
  851. * a directory inode along with a dentry.
  852. * and/or a target inode
  853. *
  854. * Called with snap_rwsem (read).
  855. */
  856. int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req,
  857. struct ceph_mds_session *session)
  858. {
  859. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  860. struct inode *in = NULL;
  861. struct ceph_mds_reply_inode *ininfo;
  862. struct ceph_vino vino;
  863. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  864. int i = 0;
  865. int err = 0;
  866. dout("fill_trace %p is_dentry %d is_target %d\n", req,
  867. rinfo->head->is_dentry, rinfo->head->is_target);
  868. #if 0
  869. /*
  870. * Debugging hook:
  871. *
  872. * If we resend completed ops to a recovering mds, we get no
  873. * trace. Since that is very rare, pretend this is the case
  874. * to ensure the 'no trace' handlers in the callers behave.
  875. *
  876. * Fill in inodes unconditionally to avoid breaking cap
  877. * invariants.
  878. */
  879. if (rinfo->head->op & CEPH_MDS_OP_WRITE) {
  880. pr_info("fill_trace faking empty trace on %lld %s\n",
  881. req->r_tid, ceph_mds_op_name(rinfo->head->op));
  882. if (rinfo->head->is_dentry) {
  883. rinfo->head->is_dentry = 0;
  884. err = fill_inode(req->r_locked_dir,
  885. &rinfo->diri, rinfo->dirfrag,
  886. session, req->r_request_started, -1);
  887. }
  888. if (rinfo->head->is_target) {
  889. rinfo->head->is_target = 0;
  890. ininfo = rinfo->targeti.in;
  891. vino.ino = le64_to_cpu(ininfo->ino);
  892. vino.snap = le64_to_cpu(ininfo->snapid);
  893. in = ceph_get_inode(sb, vino);
  894. err = fill_inode(in, &rinfo->targeti, NULL,
  895. session, req->r_request_started,
  896. req->r_fmode);
  897. iput(in);
  898. }
  899. }
  900. #endif
  901. if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
  902. dout("fill_trace reply is empty!\n");
  903. if (rinfo->head->result == 0 && req->r_locked_dir)
  904. ceph_invalidate_dir_request(req);
  905. return 0;
  906. }
  907. if (rinfo->head->is_dentry) {
  908. struct inode *dir = req->r_locked_dir;
  909. if (dir) {
  910. err = fill_inode(dir, &rinfo->diri, rinfo->dirfrag,
  911. session, req->r_request_started, -1,
  912. &req->r_caps_reservation);
  913. if (err < 0)
  914. return err;
  915. } else {
  916. WARN_ON_ONCE(1);
  917. }
  918. }
  919. /*
  920. * ignore null lease/binding on snapdir ENOENT, or else we
  921. * will have trouble splicing in the virtual snapdir later
  922. */
  923. if (rinfo->head->is_dentry && !req->r_aborted &&
  924. req->r_locked_dir &&
  925. (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
  926. fsc->mount_options->snapdir_name,
  927. req->r_dentry->d_name.len))) {
  928. /*
  929. * lookup link rename : null -> possibly existing inode
  930. * mknod symlink mkdir : null -> new inode
  931. * unlink : linked -> null
  932. */
  933. struct inode *dir = req->r_locked_dir;
  934. struct dentry *dn = req->r_dentry;
  935. bool have_dir_cap, have_lease;
  936. BUG_ON(!dn);
  937. BUG_ON(!dir);
  938. BUG_ON(dn->d_parent->d_inode != dir);
  939. BUG_ON(ceph_ino(dir) !=
  940. le64_to_cpu(rinfo->diri.in->ino));
  941. BUG_ON(ceph_snap(dir) !=
  942. le64_to_cpu(rinfo->diri.in->snapid));
  943. /* do we have a lease on the whole dir? */
  944. have_dir_cap =
  945. (le32_to_cpu(rinfo->diri.in->cap.caps) &
  946. CEPH_CAP_FILE_SHARED);
  947. /* do we have a dn lease? */
  948. have_lease = have_dir_cap ||
  949. le32_to_cpu(rinfo->dlease->duration_ms);
  950. if (!have_lease)
  951. dout("fill_trace no dentry lease or dir cap\n");
  952. /* rename? */
  953. if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
  954. dout(" src %p '%.*s' dst %p '%.*s'\n",
  955. req->r_old_dentry,
  956. req->r_old_dentry->d_name.len,
  957. req->r_old_dentry->d_name.name,
  958. dn, dn->d_name.len, dn->d_name.name);
  959. dout("fill_trace doing d_move %p -> %p\n",
  960. req->r_old_dentry, dn);
  961. d_move(req->r_old_dentry, dn);
  962. dout(" src %p '%.*s' dst %p '%.*s'\n",
  963. req->r_old_dentry,
  964. req->r_old_dentry->d_name.len,
  965. req->r_old_dentry->d_name.name,
  966. dn, dn->d_name.len, dn->d_name.name);
  967. /* ensure target dentry is invalidated, despite
  968. rehashing bug in vfs_rename_dir */
  969. ceph_invalidate_dentry_lease(dn);
  970. /*
  971. * d_move() puts the renamed dentry at the end of
  972. * d_subdirs. We need to assign it an appropriate
  973. * directory offset so we can behave when dir is
  974. * complete.
  975. */
  976. ceph_set_dentry_offset(req->r_old_dentry);
  977. dout("dn %p gets new offset %lld\n", req->r_old_dentry,
  978. ceph_dentry(req->r_old_dentry)->offset);
  979. dn = req->r_old_dentry; /* use old_dentry */
  980. in = dn->d_inode;
  981. }
  982. /* null dentry? */
  983. if (!rinfo->head->is_target) {
  984. dout("fill_trace null dentry\n");
  985. if (dn->d_inode) {
  986. dout("d_delete %p\n", dn);
  987. d_delete(dn);
  988. } else {
  989. dout("d_instantiate %p NULL\n", dn);
  990. d_instantiate(dn, NULL);
  991. if (have_lease && d_unhashed(dn))
  992. d_rehash(dn);
  993. update_dentry_lease(dn, rinfo->dlease,
  994. session,
  995. req->r_request_started);
  996. }
  997. goto done;
  998. }
  999. /* attach proper inode */
  1000. ininfo = rinfo->targeti.in;
  1001. vino.ino = le64_to_cpu(ininfo->ino);
  1002. vino.snap = le64_to_cpu(ininfo->snapid);
  1003. in = dn->d_inode;
  1004. if (!in) {
  1005. in = ceph_get_inode(sb, vino);
  1006. if (IS_ERR(in)) {
  1007. pr_err("fill_trace bad get_inode "
  1008. "%llx.%llx\n", vino.ino, vino.snap);
  1009. err = PTR_ERR(in);
  1010. d_drop(dn);
  1011. goto done;
  1012. }
  1013. dn = splice_dentry(dn, in, &have_lease, true);
  1014. if (IS_ERR(dn)) {
  1015. err = PTR_ERR(dn);
  1016. goto done;
  1017. }
  1018. req->r_dentry = dn; /* may have spliced */
  1019. ihold(in);
  1020. } else if (ceph_ino(in) == vino.ino &&
  1021. ceph_snap(in) == vino.snap) {
  1022. ihold(in);
  1023. } else {
  1024. dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
  1025. dn, in, ceph_ino(in), ceph_snap(in),
  1026. vino.ino, vino.snap);
  1027. have_lease = false;
  1028. in = NULL;
  1029. }
  1030. if (have_lease)
  1031. update_dentry_lease(dn, rinfo->dlease, session,
  1032. req->r_request_started);
  1033. dout(" final dn %p\n", dn);
  1034. i++;
  1035. } else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
  1036. req->r_op == CEPH_MDS_OP_MKSNAP) && !req->r_aborted) {
  1037. struct dentry *dn = req->r_dentry;
  1038. /* fill out a snapdir LOOKUPSNAP dentry */
  1039. BUG_ON(!dn);
  1040. BUG_ON(!req->r_locked_dir);
  1041. BUG_ON(ceph_snap(req->r_locked_dir) != CEPH_SNAPDIR);
  1042. ininfo = rinfo->targeti.in;
  1043. vino.ino = le64_to_cpu(ininfo->ino);
  1044. vino.snap = le64_to_cpu(ininfo->snapid);
  1045. in = ceph_get_inode(sb, vino);
  1046. if (IS_ERR(in)) {
  1047. pr_err("fill_inode get_inode badness %llx.%llx\n",
  1048. vino.ino, vino.snap);
  1049. err = PTR_ERR(in);
  1050. d_delete(dn);
  1051. goto done;
  1052. }
  1053. dout(" linking snapped dir %p to dn %p\n", in, dn);
  1054. dn = splice_dentry(dn, in, NULL, true);
  1055. if (IS_ERR(dn)) {
  1056. err = PTR_ERR(dn);
  1057. goto done;
  1058. }
  1059. req->r_dentry = dn; /* may have spliced */
  1060. ihold(in);
  1061. rinfo->head->is_dentry = 1; /* fool notrace handlers */
  1062. }
  1063. if (rinfo->head->is_target) {
  1064. vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
  1065. vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
  1066. if (in == NULL || ceph_ino(in) != vino.ino ||
  1067. ceph_snap(in) != vino.snap) {
  1068. in = ceph_get_inode(sb, vino);
  1069. if (IS_ERR(in)) {
  1070. err = PTR_ERR(in);
  1071. goto done;
  1072. }
  1073. }
  1074. req->r_target_inode = in;
  1075. err = fill_inode(in,
  1076. &rinfo->targeti, NULL,
  1077. session, req->r_request_started,
  1078. (le32_to_cpu(rinfo->head->result) == 0) ?
  1079. req->r_fmode : -1,
  1080. &req->r_caps_reservation);
  1081. if (err < 0) {
  1082. pr_err("fill_inode badness %p %llx.%llx\n",
  1083. in, ceph_vinop(in));
  1084. goto done;
  1085. }
  1086. }
  1087. done:
  1088. dout("fill_trace done err=%d\n", err);
  1089. return err;
  1090. }
  1091. /*
  1092. * Prepopulate our cache with readdir results, leases, etc.
  1093. */
  1094. static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req,
  1095. struct ceph_mds_session *session)
  1096. {
  1097. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  1098. int i, err = 0;
  1099. for (i = 0; i < rinfo->dir_nr; i++) {
  1100. struct ceph_vino vino;
  1101. struct inode *in;
  1102. int rc;
  1103. vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
  1104. vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
  1105. in = ceph_get_inode(req->r_dentry->d_sb, vino);
  1106. if (IS_ERR(in)) {
  1107. err = PTR_ERR(in);
  1108. dout("new_inode badness got %d\n", err);
  1109. continue;
  1110. }
  1111. rc = fill_inode(in, &rinfo->dir_in[i], NULL, session,
  1112. req->r_request_started, -1,
  1113. &req->r_caps_reservation);
  1114. if (rc < 0) {
  1115. pr_err("fill_inode badness on %p got %d\n", in, rc);
  1116. err = rc;
  1117. continue;
  1118. }
  1119. }
  1120. return err;
  1121. }
  1122. int ceph_readdir_prepopulate(struct ceph_mds_request *req,
  1123. struct ceph_mds_session *session)
  1124. {
  1125. struct dentry *parent = req->r_dentry;
  1126. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  1127. struct qstr dname;
  1128. struct dentry *dn;
  1129. struct inode *in;
  1130. int err = 0, i;
  1131. struct inode *snapdir = NULL;
  1132. struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
  1133. struct ceph_dentry_info *di;
  1134. u64 r_readdir_offset = req->r_readdir_offset;
  1135. u32 frag = le32_to_cpu(rhead->args.readdir.frag);
  1136. if (rinfo->dir_dir &&
  1137. le32_to_cpu(rinfo->dir_dir->frag) != frag) {
  1138. dout("readdir_prepopulate got new frag %x -> %x\n",
  1139. frag, le32_to_cpu(rinfo->dir_dir->frag));
  1140. frag = le32_to_cpu(rinfo->dir_dir->frag);
  1141. if (ceph_frag_is_leftmost(frag))
  1142. r_readdir_offset = 2;
  1143. else
  1144. r_readdir_offset = 0;
  1145. }
  1146. if (req->r_aborted)
  1147. return readdir_prepopulate_inodes_only(req, session);
  1148. if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
  1149. snapdir = ceph_get_snapdir(parent->d_inode);
  1150. parent = d_find_alias(snapdir);
  1151. dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
  1152. rinfo->dir_nr, parent);
  1153. } else {
  1154. dout("readdir_prepopulate %d items under dn %p\n",
  1155. rinfo->dir_nr, parent);
  1156. if (rinfo->dir_dir)
  1157. ceph_fill_dirfrag(parent->d_inode, rinfo->dir_dir);
  1158. }
  1159. for (i = 0; i < rinfo->dir_nr; i++) {
  1160. struct ceph_vino vino;
  1161. dname.name = rinfo->dir_dname[i];
  1162. dname.len = rinfo->dir_dname_len[i];
  1163. dname.hash = full_name_hash(dname.name, dname.len);
  1164. vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
  1165. vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
  1166. retry_lookup:
  1167. dn = d_lookup(parent, &dname);
  1168. dout("d_lookup on parent=%p name=%.*s got %p\n",
  1169. parent, dname.len, dname.name, dn);
  1170. if (!dn) {
  1171. dn = d_alloc(parent, &dname);
  1172. dout("d_alloc %p '%.*s' = %p\n", parent,
  1173. dname.len, dname.name, dn);
  1174. if (dn == NULL) {
  1175. dout("d_alloc badness\n");
  1176. err = -ENOMEM;
  1177. goto out;
  1178. }
  1179. err = ceph_init_dentry(dn);
  1180. if (err < 0) {
  1181. dput(dn);
  1182. goto out;
  1183. }
  1184. } else if (dn->d_inode &&
  1185. (ceph_ino(dn->d_inode) != vino.ino ||
  1186. ceph_snap(dn->d_inode) != vino.snap)) {
  1187. dout(" dn %p points to wrong inode %p\n",
  1188. dn, dn->d_inode);
  1189. d_delete(dn);
  1190. dput(dn);
  1191. goto retry_lookup;
  1192. } else {
  1193. /* reorder parent's d_subdirs */
  1194. spin_lock(&parent->d_lock);
  1195. spin_lock_nested(&dn->d_lock, DENTRY_D_LOCK_NESTED);
  1196. list_move(&dn->d_u.d_child, &parent->d_subdirs);
  1197. spin_unlock(&dn->d_lock);
  1198. spin_unlock(&parent->d_lock);
  1199. }
  1200. di = dn->d_fsdata;
  1201. di->offset = ceph_make_fpos(frag, i + r_readdir_offset);
  1202. /* inode */
  1203. if (dn->d_inode) {
  1204. in = dn->d_inode;
  1205. } else {
  1206. in = ceph_get_inode(parent->d_sb, vino);
  1207. if (IS_ERR(in)) {
  1208. dout("new_inode badness\n");
  1209. d_drop(dn);
  1210. dput(dn);
  1211. err = PTR_ERR(in);
  1212. goto out;
  1213. }
  1214. dn = splice_dentry(dn, in, NULL, false);
  1215. if (IS_ERR(dn))
  1216. dn = NULL;
  1217. }
  1218. if (fill_inode(in, &rinfo->dir_in[i], NULL, session,
  1219. req->r_request_started, -1,
  1220. &req->r_caps_reservation) < 0) {
  1221. pr_err("fill_inode badness on %p\n", in);
  1222. goto next_item;
  1223. }
  1224. if (dn)
  1225. update_dentry_lease(dn, rinfo->dir_dlease[i],
  1226. req->r_session,
  1227. req->r_request_started);
  1228. next_item:
  1229. if (dn)
  1230. dput(dn);
  1231. }
  1232. req->r_did_prepopulate = true;
  1233. out:
  1234. if (snapdir) {
  1235. iput(snapdir);
  1236. dput(parent);
  1237. }
  1238. dout("readdir_prepopulate done\n");
  1239. return err;
  1240. }
  1241. int ceph_inode_set_size(struct inode *inode, loff_t size)
  1242. {
  1243. struct ceph_inode_info *ci = ceph_inode(inode);
  1244. int ret = 0;
  1245. spin_lock(&ci->i_ceph_lock);
  1246. dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
  1247. inode->i_size = size;
  1248. inode->i_blocks = (size + (1 << 9) - 1) >> 9;
  1249. /* tell the MDS if we are approaching max_size */
  1250. if ((size << 1) >= ci->i_max_size &&
  1251. (ci->i_reported_size << 1) < ci->i_max_size)
  1252. ret = 1;
  1253. spin_unlock(&ci->i_ceph_lock);
  1254. return ret;
  1255. }
  1256. /*
  1257. * Write back inode data in a worker thread. (This can't be done
  1258. * in the message handler context.)
  1259. */
  1260. void ceph_queue_writeback(struct inode *inode)
  1261. {
  1262. ihold(inode);
  1263. if (queue_work(ceph_inode_to_client(inode)->wb_wq,
  1264. &ceph_inode(inode)->i_wb_work)) {
  1265. dout("ceph_queue_writeback %p\n", inode);
  1266. } else {
  1267. dout("ceph_queue_writeback %p failed\n", inode);
  1268. iput(inode);
  1269. }
  1270. }
  1271. static void ceph_writeback_work(struct work_struct *work)
  1272. {
  1273. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1274. i_wb_work);
  1275. struct inode *inode = &ci->vfs_inode;
  1276. dout("writeback %p\n", inode);
  1277. filemap_fdatawrite(&inode->i_data);
  1278. iput(inode);
  1279. }
  1280. /*
  1281. * queue an async invalidation
  1282. */
  1283. void ceph_queue_invalidate(struct inode *inode)
  1284. {
  1285. ihold(inode);
  1286. if (queue_work(ceph_inode_to_client(inode)->pg_inv_wq,
  1287. &ceph_inode(inode)->i_pg_inv_work)) {
  1288. dout("ceph_queue_invalidate %p\n", inode);
  1289. } else {
  1290. dout("ceph_queue_invalidate %p failed\n", inode);
  1291. iput(inode);
  1292. }
  1293. }
  1294. /*
  1295. * Invalidate inode pages in a worker thread. (This can't be done
  1296. * in the message handler context.)
  1297. */
  1298. static void ceph_invalidate_work(struct work_struct *work)
  1299. {
  1300. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1301. i_pg_inv_work);
  1302. struct inode *inode = &ci->vfs_inode;
  1303. u32 orig_gen;
  1304. int check = 0;
  1305. mutex_lock(&ci->i_truncate_mutex);
  1306. spin_lock(&ci->i_ceph_lock);
  1307. dout("invalidate_pages %p gen %d revoking %d\n", inode,
  1308. ci->i_rdcache_gen, ci->i_rdcache_revoking);
  1309. if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
  1310. /* nevermind! */
  1311. spin_unlock(&ci->i_ceph_lock);
  1312. mutex_unlock(&ci->i_truncate_mutex);
  1313. goto out;
  1314. }
  1315. orig_gen = ci->i_rdcache_gen;
  1316. spin_unlock(&ci->i_ceph_lock);
  1317. truncate_inode_pages(inode->i_mapping, 0);
  1318. spin_lock(&ci->i_ceph_lock);
  1319. if (orig_gen == ci->i_rdcache_gen &&
  1320. orig_gen == ci->i_rdcache_revoking) {
  1321. dout("invalidate_pages %p gen %d successful\n", inode,
  1322. ci->i_rdcache_gen);
  1323. ci->i_rdcache_revoking--;
  1324. check = 1;
  1325. } else {
  1326. dout("invalidate_pages %p gen %d raced, now %d revoking %d\n",
  1327. inode, orig_gen, ci->i_rdcache_gen,
  1328. ci->i_rdcache_revoking);
  1329. }
  1330. spin_unlock(&ci->i_ceph_lock);
  1331. mutex_unlock(&ci->i_truncate_mutex);
  1332. if (check)
  1333. ceph_check_caps(ci, 0, NULL);
  1334. out:
  1335. iput(inode);
  1336. }
  1337. /*
  1338. * called by trunc_wq;
  1339. *
  1340. * We also truncate in a separate thread as well.
  1341. */
  1342. static void ceph_vmtruncate_work(struct work_struct *work)
  1343. {
  1344. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1345. i_vmtruncate_work);
  1346. struct inode *inode = &ci->vfs_inode;
  1347. dout("vmtruncate_work %p\n", inode);
  1348. __ceph_do_pending_vmtruncate(inode);
  1349. iput(inode);
  1350. }
  1351. /*
  1352. * Queue an async vmtruncate. If we fail to queue work, we will handle
  1353. * the truncation the next time we call __ceph_do_pending_vmtruncate.
  1354. */
  1355. void ceph_queue_vmtruncate(struct inode *inode)
  1356. {
  1357. struct ceph_inode_info *ci = ceph_inode(inode);
  1358. ihold(inode);
  1359. if (queue_work(ceph_sb_to_client(inode->i_sb)->trunc_wq,
  1360. &ci->i_vmtruncate_work)) {
  1361. dout("ceph_queue_vmtruncate %p\n", inode);
  1362. } else {
  1363. dout("ceph_queue_vmtruncate %p failed, pending=%d\n",
  1364. inode, ci->i_truncate_pending);
  1365. iput(inode);
  1366. }
  1367. }
  1368. /*
  1369. * Make sure any pending truncation is applied before doing anything
  1370. * that may depend on it.
  1371. */
  1372. void __ceph_do_pending_vmtruncate(struct inode *inode)
  1373. {
  1374. struct ceph_inode_info *ci = ceph_inode(inode);
  1375. u64 to;
  1376. int wrbuffer_refs, finish = 0;
  1377. mutex_lock(&ci->i_truncate_mutex);
  1378. retry:
  1379. spin_lock(&ci->i_ceph_lock);
  1380. if (ci->i_truncate_pending == 0) {
  1381. dout("__do_pending_vmtruncate %p none pending\n", inode);
  1382. spin_unlock(&ci->i_ceph_lock);
  1383. mutex_unlock(&ci->i_truncate_mutex);
  1384. return;
  1385. }
  1386. /*
  1387. * make sure any dirty snapped pages are flushed before we
  1388. * possibly truncate them.. so write AND block!
  1389. */
  1390. if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
  1391. dout("__do_pending_vmtruncate %p flushing snaps first\n",
  1392. inode);
  1393. spin_unlock(&ci->i_ceph_lock);
  1394. filemap_write_and_wait_range(&inode->i_data, 0,
  1395. inode->i_sb->s_maxbytes);
  1396. goto retry;
  1397. }
  1398. /* there should be no reader or writer */
  1399. WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref);
  1400. to = ci->i_truncate_size;
  1401. wrbuffer_refs = ci->i_wrbuffer_ref;
  1402. dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
  1403. ci->i_truncate_pending, to);
  1404. spin_unlock(&ci->i_ceph_lock);
  1405. truncate_inode_pages(inode->i_mapping, to);
  1406. spin_lock(&ci->i_ceph_lock);
  1407. if (to == ci->i_truncate_size) {
  1408. ci->i_truncate_pending = 0;
  1409. finish = 1;
  1410. }
  1411. spin_unlock(&ci->i_ceph_lock);
  1412. if (!finish)
  1413. goto retry;
  1414. mutex_unlock(&ci->i_truncate_mutex);
  1415. if (wrbuffer_refs == 0)
  1416. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  1417. wake_up_all(&ci->i_cap_wq);
  1418. }
  1419. /*
  1420. * symlinks
  1421. */
  1422. static void *ceph_sym_follow_link(struct dentry *dentry, struct nameidata *nd)
  1423. {
  1424. struct ceph_inode_info *ci = ceph_inode(dentry->d_inode);
  1425. nd_set_link(nd, ci->i_symlink);
  1426. return NULL;
  1427. }
  1428. static const struct inode_operations ceph_symlink_iops = {
  1429. .readlink = generic_readlink,
  1430. .follow_link = ceph_sym_follow_link,
  1431. .setattr = ceph_setattr,
  1432. .getattr = ceph_getattr,
  1433. .setxattr = ceph_setxattr,
  1434. .getxattr = ceph_getxattr,
  1435. .listxattr = ceph_listxattr,
  1436. .removexattr = ceph_removexattr,
  1437. };
  1438. /*
  1439. * setattr
  1440. */
  1441. int ceph_setattr(struct dentry *dentry, struct iattr *attr)
  1442. {
  1443. struct inode *inode = dentry->d_inode;
  1444. struct ceph_inode_info *ci = ceph_inode(inode);
  1445. struct inode *parent_inode;
  1446. const unsigned int ia_valid = attr->ia_valid;
  1447. struct ceph_mds_request *req;
  1448. struct ceph_mds_client *mdsc = ceph_sb_to_client(dentry->d_sb)->mdsc;
  1449. int issued;
  1450. int release = 0, dirtied = 0;
  1451. int mask = 0;
  1452. int err = 0;
  1453. int inode_dirty_flags = 0;
  1454. if (ceph_snap(inode) != CEPH_NOSNAP)
  1455. return -EROFS;
  1456. err = inode_change_ok(inode, attr);
  1457. if (err != 0)
  1458. return err;
  1459. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
  1460. USE_AUTH_MDS);
  1461. if (IS_ERR(req))
  1462. return PTR_ERR(req);
  1463. spin_lock(&ci->i_ceph_lock);
  1464. issued = __ceph_caps_issued(ci, NULL);
  1465. dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
  1466. if (ia_valid & ATTR_UID) {
  1467. dout("setattr %p uid %d -> %d\n", inode,
  1468. from_kuid(&init_user_ns, inode->i_uid),
  1469. from_kuid(&init_user_ns, attr->ia_uid));
  1470. if (issued & CEPH_CAP_AUTH_EXCL) {
  1471. inode->i_uid = attr->ia_uid;
  1472. dirtied |= CEPH_CAP_AUTH_EXCL;
  1473. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1474. !uid_eq(attr->ia_uid, inode->i_uid)) {
  1475. req->r_args.setattr.uid = cpu_to_le32(
  1476. from_kuid(&init_user_ns, attr->ia_uid));
  1477. mask |= CEPH_SETATTR_UID;
  1478. release |= CEPH_CAP_AUTH_SHARED;
  1479. }
  1480. }
  1481. if (ia_valid & ATTR_GID) {
  1482. dout("setattr %p gid %d -> %d\n", inode,
  1483. from_kgid(&init_user_ns, inode->i_gid),
  1484. from_kgid(&init_user_ns, attr->ia_gid));
  1485. if (issued & CEPH_CAP_AUTH_EXCL) {
  1486. inode->i_gid = attr->ia_gid;
  1487. dirtied |= CEPH_CAP_AUTH_EXCL;
  1488. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1489. !gid_eq(attr->ia_gid, inode->i_gid)) {
  1490. req->r_args.setattr.gid = cpu_to_le32(
  1491. from_kgid(&init_user_ns, attr->ia_gid));
  1492. mask |= CEPH_SETATTR_GID;
  1493. release |= CEPH_CAP_AUTH_SHARED;
  1494. }
  1495. }
  1496. if (ia_valid & ATTR_MODE) {
  1497. dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
  1498. attr->ia_mode);
  1499. if (issued & CEPH_CAP_AUTH_EXCL) {
  1500. inode->i_mode = attr->ia_mode;
  1501. dirtied |= CEPH_CAP_AUTH_EXCL;
  1502. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1503. attr->ia_mode != inode->i_mode) {
  1504. req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
  1505. mask |= CEPH_SETATTR_MODE;
  1506. release |= CEPH_CAP_AUTH_SHARED;
  1507. }
  1508. }
  1509. if (ia_valid & ATTR_ATIME) {
  1510. dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode,
  1511. inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
  1512. attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
  1513. if (issued & CEPH_CAP_FILE_EXCL) {
  1514. ci->i_time_warp_seq++;
  1515. inode->i_atime = attr->ia_atime;
  1516. dirtied |= CEPH_CAP_FILE_EXCL;
  1517. } else if ((issued & CEPH_CAP_FILE_WR) &&
  1518. timespec_compare(&inode->i_atime,
  1519. &attr->ia_atime) < 0) {
  1520. inode->i_atime = attr->ia_atime;
  1521. dirtied |= CEPH_CAP_FILE_WR;
  1522. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1523. !timespec_equal(&inode->i_atime, &attr->ia_atime)) {
  1524. ceph_encode_timespec(&req->r_args.setattr.atime,
  1525. &attr->ia_atime);
  1526. mask |= CEPH_SETATTR_ATIME;
  1527. release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD |
  1528. CEPH_CAP_FILE_WR;
  1529. }
  1530. }
  1531. if (ia_valid & ATTR_MTIME) {
  1532. dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode,
  1533. inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
  1534. attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
  1535. if (issued & CEPH_CAP_FILE_EXCL) {
  1536. ci->i_time_warp_seq++;
  1537. inode->i_mtime = attr->ia_mtime;
  1538. dirtied |= CEPH_CAP_FILE_EXCL;
  1539. } else if ((issued & CEPH_CAP_FILE_WR) &&
  1540. timespec_compare(&inode->i_mtime,
  1541. &attr->ia_mtime) < 0) {
  1542. inode->i_mtime = attr->ia_mtime;
  1543. dirtied |= CEPH_CAP_FILE_WR;
  1544. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1545. !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) {
  1546. ceph_encode_timespec(&req->r_args.setattr.mtime,
  1547. &attr->ia_mtime);
  1548. mask |= CEPH_SETATTR_MTIME;
  1549. release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
  1550. CEPH_CAP_FILE_WR;
  1551. }
  1552. }
  1553. if (ia_valid & ATTR_SIZE) {
  1554. dout("setattr %p size %lld -> %lld\n", inode,
  1555. inode->i_size, attr->ia_size);
  1556. if (attr->ia_size > inode->i_sb->s_maxbytes) {
  1557. err = -EINVAL;
  1558. goto out;
  1559. }
  1560. if ((issued & CEPH_CAP_FILE_EXCL) &&
  1561. attr->ia_size > inode->i_size) {
  1562. inode->i_size = attr->ia_size;
  1563. inode->i_blocks =
  1564. (attr->ia_size + (1 << 9) - 1) >> 9;
  1565. inode->i_ctime = attr->ia_ctime;
  1566. ci->i_reported_size = attr->ia_size;
  1567. dirtied |= CEPH_CAP_FILE_EXCL;
  1568. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1569. attr->ia_size != inode->i_size) {
  1570. req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
  1571. req->r_args.setattr.old_size =
  1572. cpu_to_le64(inode->i_size);
  1573. mask |= CEPH_SETATTR_SIZE;
  1574. release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
  1575. CEPH_CAP_FILE_WR;
  1576. }
  1577. }
  1578. /* these do nothing */
  1579. if (ia_valid & ATTR_CTIME) {
  1580. bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
  1581. ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
  1582. dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode,
  1583. inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
  1584. attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
  1585. only ? "ctime only" : "ignored");
  1586. inode->i_ctime = attr->ia_ctime;
  1587. if (only) {
  1588. /*
  1589. * if kernel wants to dirty ctime but nothing else,
  1590. * we need to choose a cap to dirty under, or do
  1591. * a almost-no-op setattr
  1592. */
  1593. if (issued & CEPH_CAP_AUTH_EXCL)
  1594. dirtied |= CEPH_CAP_AUTH_EXCL;
  1595. else if (issued & CEPH_CAP_FILE_EXCL)
  1596. dirtied |= CEPH_CAP_FILE_EXCL;
  1597. else if (issued & CEPH_CAP_XATTR_EXCL)
  1598. dirtied |= CEPH_CAP_XATTR_EXCL;
  1599. else
  1600. mask |= CEPH_SETATTR_CTIME;
  1601. }
  1602. }
  1603. if (ia_valid & ATTR_FILE)
  1604. dout("setattr %p ATTR_FILE ... hrm!\n", inode);
  1605. if (dirtied) {
  1606. inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied);
  1607. inode->i_ctime = CURRENT_TIME;
  1608. }
  1609. release &= issued;
  1610. spin_unlock(&ci->i_ceph_lock);
  1611. if (inode_dirty_flags)
  1612. __mark_inode_dirty(inode, inode_dirty_flags);
  1613. if (mask) {
  1614. req->r_inode = inode;
  1615. ihold(inode);
  1616. req->r_inode_drop = release;
  1617. req->r_args.setattr.mask = cpu_to_le32(mask);
  1618. req->r_num_caps = 1;
  1619. parent_inode = ceph_get_dentry_parent_inode(dentry);
  1620. err = ceph_mdsc_do_request(mdsc, parent_inode, req);
  1621. iput(parent_inode);
  1622. }
  1623. dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
  1624. ceph_cap_string(dirtied), mask);
  1625. ceph_mdsc_put_request(req);
  1626. if (mask & CEPH_SETATTR_SIZE)
  1627. __ceph_do_pending_vmtruncate(inode);
  1628. return err;
  1629. out:
  1630. spin_unlock(&ci->i_ceph_lock);
  1631. ceph_mdsc_put_request(req);
  1632. return err;
  1633. }
  1634. /*
  1635. * Verify that we have a lease on the given mask. If not,
  1636. * do a getattr against an mds.
  1637. */
  1638. int ceph_do_getattr(struct inode *inode, int mask)
  1639. {
  1640. struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
  1641. struct ceph_mds_client *mdsc = fsc->mdsc;
  1642. struct ceph_mds_request *req;
  1643. int err;
  1644. if (ceph_snap(inode) == CEPH_SNAPDIR) {
  1645. dout("do_getattr inode %p SNAPDIR\n", inode);
  1646. return 0;
  1647. }
  1648. dout("do_getattr inode %p mask %s mode 0%o\n", inode, ceph_cap_string(mask), inode->i_mode);
  1649. if (ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
  1650. return 0;
  1651. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
  1652. if (IS_ERR(req))
  1653. return PTR_ERR(req);
  1654. req->r_inode = inode;
  1655. ihold(inode);
  1656. req->r_num_caps = 1;
  1657. req->r_args.getattr.mask = cpu_to_le32(mask);
  1658. err = ceph_mdsc_do_request(mdsc, NULL, req);
  1659. ceph_mdsc_put_request(req);
  1660. dout("do_getattr result=%d\n", err);
  1661. return err;
  1662. }
  1663. /*
  1664. * Check inode permissions. We verify we have a valid value for
  1665. * the AUTH cap, then call the generic handler.
  1666. */
  1667. int ceph_permission(struct inode *inode, int mask)
  1668. {
  1669. int err;
  1670. if (mask & MAY_NOT_BLOCK)
  1671. return -ECHILD;
  1672. err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED);
  1673. if (!err)
  1674. err = generic_permission(inode, mask);
  1675. return err;
  1676. }
  1677. /*
  1678. * Get all attributes. Hopefully somedata we'll have a statlite()
  1679. * and can limit the fields we require to be accurate.
  1680. */
  1681. int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1682. struct kstat *stat)
  1683. {
  1684. struct inode *inode = dentry->d_inode;
  1685. struct ceph_inode_info *ci = ceph_inode(inode);
  1686. int err;
  1687. err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL);
  1688. if (!err) {
  1689. generic_fillattr(inode, stat);
  1690. stat->ino = ceph_translate_ino(inode->i_sb, inode->i_ino);
  1691. if (ceph_snap(inode) != CEPH_NOSNAP)
  1692. stat->dev = ceph_snap(inode);
  1693. else
  1694. stat->dev = 0;
  1695. if (S_ISDIR(inode->i_mode)) {
  1696. if (ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb),
  1697. RBYTES))
  1698. stat->size = ci->i_rbytes;
  1699. else
  1700. stat->size = ci->i_files + ci->i_subdirs;
  1701. stat->blocks = 0;
  1702. stat->blksize = 65536;
  1703. }
  1704. }
  1705. return err;
  1706. }