inode.c 53 KB

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