inode.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  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. /* only update max_size on auth cap */
  580. if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
  581. ci->i_max_size != le64_to_cpu(info->max_size)) {
  582. dout("max_size %lld -> %llu\n", ci->i_max_size,
  583. le64_to_cpu(info->max_size));
  584. ci->i_max_size = le64_to_cpu(info->max_size);
  585. }
  586. ci->i_layout = info->layout;
  587. inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
  588. /* xattrs */
  589. /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
  590. if ((issued & CEPH_CAP_XATTR_EXCL) == 0 &&
  591. le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
  592. if (ci->i_xattrs.blob)
  593. ceph_buffer_put(ci->i_xattrs.blob);
  594. ci->i_xattrs.blob = xattr_blob;
  595. if (xattr_blob)
  596. memcpy(ci->i_xattrs.blob->vec.iov_base,
  597. iinfo->xattr_data, iinfo->xattr_len);
  598. ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
  599. ceph_forget_all_cached_acls(inode);
  600. xattr_blob = NULL;
  601. }
  602. inode->i_mapping->a_ops = &ceph_aops;
  603. inode->i_mapping->backing_dev_info =
  604. &ceph_sb_to_client(inode->i_sb)->backing_dev_info;
  605. switch (inode->i_mode & S_IFMT) {
  606. case S_IFIFO:
  607. case S_IFBLK:
  608. case S_IFCHR:
  609. case S_IFSOCK:
  610. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  611. inode->i_op = &ceph_file_iops;
  612. break;
  613. case S_IFREG:
  614. inode->i_op = &ceph_file_iops;
  615. inode->i_fop = &ceph_file_fops;
  616. break;
  617. case S_IFLNK:
  618. inode->i_op = &ceph_symlink_iops;
  619. if (!ci->i_symlink) {
  620. u32 symlen = iinfo->symlink_len;
  621. char *sym;
  622. spin_unlock(&ci->i_ceph_lock);
  623. err = -EINVAL;
  624. if (WARN_ON(symlen != inode->i_size))
  625. goto out;
  626. err = -ENOMEM;
  627. sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS);
  628. if (!sym)
  629. goto out;
  630. spin_lock(&ci->i_ceph_lock);
  631. if (!ci->i_symlink)
  632. ci->i_symlink = sym;
  633. else
  634. kfree(sym); /* lost a race */
  635. }
  636. break;
  637. case S_IFDIR:
  638. inode->i_op = &ceph_dir_iops;
  639. inode->i_fop = &ceph_dir_fops;
  640. ci->i_dir_layout = iinfo->dir_layout;
  641. ci->i_files = le64_to_cpu(info->files);
  642. ci->i_subdirs = le64_to_cpu(info->subdirs);
  643. ci->i_rbytes = le64_to_cpu(info->rbytes);
  644. ci->i_rfiles = le64_to_cpu(info->rfiles);
  645. ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
  646. ceph_decode_timespec(&ci->i_rctime, &info->rctime);
  647. break;
  648. default:
  649. pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
  650. ceph_vinop(inode), inode->i_mode);
  651. }
  652. /* set dir completion flag? */
  653. if (S_ISDIR(inode->i_mode) &&
  654. ci->i_files == 0 && ci->i_subdirs == 0 &&
  655. ceph_snap(inode) == CEPH_NOSNAP &&
  656. (le32_to_cpu(info->cap.caps) & CEPH_CAP_FILE_SHARED) &&
  657. (issued & CEPH_CAP_FILE_EXCL) == 0 &&
  658. !__ceph_dir_is_complete(ci)) {
  659. dout(" marking %p complete (empty)\n", inode);
  660. __ceph_dir_set_complete(ci, atomic_read(&ci->i_release_count));
  661. ci->i_max_offset = 2;
  662. }
  663. no_change:
  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. return err;
  925. } else {
  926. WARN_ON_ONCE(1);
  927. }
  928. }
  929. if (rinfo->head->is_target) {
  930. vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
  931. vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
  932. in = ceph_get_inode(sb, vino);
  933. if (IS_ERR(in)) {
  934. err = PTR_ERR(in);
  935. goto done;
  936. }
  937. req->r_target_inode = in;
  938. err = fill_inode(in, &rinfo->targeti, NULL,
  939. session, req->r_request_started,
  940. (le32_to_cpu(rinfo->head->result) == 0) ?
  941. req->r_fmode : -1,
  942. &req->r_caps_reservation);
  943. if (err < 0) {
  944. pr_err("fill_inode badness %p %llx.%llx\n",
  945. in, ceph_vinop(in));
  946. goto done;
  947. }
  948. }
  949. /*
  950. * ignore null lease/binding on snapdir ENOENT, or else we
  951. * will have trouble splicing in the virtual snapdir later
  952. */
  953. if (rinfo->head->is_dentry && !req->r_aborted &&
  954. req->r_locked_dir &&
  955. (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
  956. fsc->mount_options->snapdir_name,
  957. req->r_dentry->d_name.len))) {
  958. /*
  959. * lookup link rename : null -> possibly existing inode
  960. * mknod symlink mkdir : null -> new inode
  961. * unlink : linked -> null
  962. */
  963. struct inode *dir = req->r_locked_dir;
  964. struct dentry *dn = req->r_dentry;
  965. bool have_dir_cap, have_lease;
  966. BUG_ON(!dn);
  967. BUG_ON(!dir);
  968. BUG_ON(dn->d_parent->d_inode != dir);
  969. BUG_ON(ceph_ino(dir) !=
  970. le64_to_cpu(rinfo->diri.in->ino));
  971. BUG_ON(ceph_snap(dir) !=
  972. le64_to_cpu(rinfo->diri.in->snapid));
  973. /* do we have a lease on the whole dir? */
  974. have_dir_cap =
  975. (le32_to_cpu(rinfo->diri.in->cap.caps) &
  976. CEPH_CAP_FILE_SHARED);
  977. /* do we have a dn lease? */
  978. have_lease = have_dir_cap ||
  979. le32_to_cpu(rinfo->dlease->duration_ms);
  980. if (!have_lease)
  981. dout("fill_trace no dentry lease or dir cap\n");
  982. /* rename? */
  983. if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
  984. dout(" src %p '%.*s' dst %p '%.*s'\n",
  985. req->r_old_dentry,
  986. req->r_old_dentry->d_name.len,
  987. req->r_old_dentry->d_name.name,
  988. dn, dn->d_name.len, dn->d_name.name);
  989. dout("fill_trace doing d_move %p -> %p\n",
  990. req->r_old_dentry, dn);
  991. d_move(req->r_old_dentry, dn);
  992. dout(" src %p '%.*s' dst %p '%.*s'\n",
  993. req->r_old_dentry,
  994. req->r_old_dentry->d_name.len,
  995. req->r_old_dentry->d_name.name,
  996. dn, dn->d_name.len, dn->d_name.name);
  997. /* ensure target dentry is invalidated, despite
  998. rehashing bug in vfs_rename_dir */
  999. ceph_invalidate_dentry_lease(dn);
  1000. /*
  1001. * d_move() puts the renamed dentry at the end of
  1002. * d_subdirs. We need to assign it an appropriate
  1003. * directory offset so we can behave when dir is
  1004. * complete.
  1005. */
  1006. ceph_set_dentry_offset(req->r_old_dentry);
  1007. dout("dn %p gets new offset %lld\n", req->r_old_dentry,
  1008. ceph_dentry(req->r_old_dentry)->offset);
  1009. dn = req->r_old_dentry; /* use old_dentry */
  1010. }
  1011. /* null dentry? */
  1012. if (!rinfo->head->is_target) {
  1013. dout("fill_trace null dentry\n");
  1014. if (dn->d_inode) {
  1015. dout("d_delete %p\n", dn);
  1016. d_delete(dn);
  1017. } else {
  1018. dout("d_instantiate %p NULL\n", dn);
  1019. d_instantiate(dn, NULL);
  1020. if (have_lease && d_unhashed(dn))
  1021. d_rehash(dn);
  1022. update_dentry_lease(dn, rinfo->dlease,
  1023. session,
  1024. req->r_request_started);
  1025. }
  1026. goto done;
  1027. }
  1028. /* attach proper inode */
  1029. if (!dn->d_inode) {
  1030. ihold(in);
  1031. dn = splice_dentry(dn, in, &have_lease, true);
  1032. if (IS_ERR(dn)) {
  1033. err = PTR_ERR(dn);
  1034. goto done;
  1035. }
  1036. req->r_dentry = dn; /* may have spliced */
  1037. } else if (dn->d_inode && dn->d_inode != in) {
  1038. dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
  1039. dn, dn->d_inode, ceph_vinop(dn->d_inode),
  1040. ceph_vinop(in));
  1041. have_lease = false;
  1042. }
  1043. if (have_lease)
  1044. update_dentry_lease(dn, rinfo->dlease, session,
  1045. req->r_request_started);
  1046. dout(" final dn %p\n", dn);
  1047. } else if (!req->r_aborted &&
  1048. (req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
  1049. req->r_op == CEPH_MDS_OP_MKSNAP)) {
  1050. struct dentry *dn = req->r_dentry;
  1051. /* fill out a snapdir LOOKUPSNAP dentry */
  1052. BUG_ON(!dn);
  1053. BUG_ON(!req->r_locked_dir);
  1054. BUG_ON(ceph_snap(req->r_locked_dir) != CEPH_SNAPDIR);
  1055. ininfo = rinfo->targeti.in;
  1056. vino.ino = le64_to_cpu(ininfo->ino);
  1057. vino.snap = le64_to_cpu(ininfo->snapid);
  1058. dout(" linking snapped dir %p to dn %p\n", in, dn);
  1059. ihold(in);
  1060. dn = splice_dentry(dn, in, NULL, true);
  1061. if (IS_ERR(dn)) {
  1062. err = PTR_ERR(dn);
  1063. goto done;
  1064. }
  1065. req->r_dentry = dn; /* may have spliced */
  1066. }
  1067. done:
  1068. dout("fill_trace done err=%d\n", err);
  1069. return err;
  1070. }
  1071. /*
  1072. * Prepopulate our cache with readdir results, leases, etc.
  1073. */
  1074. static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req,
  1075. struct ceph_mds_session *session)
  1076. {
  1077. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  1078. int i, err = 0;
  1079. for (i = 0; i < rinfo->dir_nr; i++) {
  1080. struct ceph_vino vino;
  1081. struct inode *in;
  1082. int rc;
  1083. vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
  1084. vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
  1085. in = ceph_get_inode(req->r_dentry->d_sb, vino);
  1086. if (IS_ERR(in)) {
  1087. err = PTR_ERR(in);
  1088. dout("new_inode badness got %d\n", err);
  1089. continue;
  1090. }
  1091. rc = fill_inode(in, &rinfo->dir_in[i], NULL, session,
  1092. req->r_request_started, -1,
  1093. &req->r_caps_reservation);
  1094. if (rc < 0) {
  1095. pr_err("fill_inode badness on %p got %d\n", in, rc);
  1096. err = rc;
  1097. continue;
  1098. }
  1099. }
  1100. return err;
  1101. }
  1102. int ceph_readdir_prepopulate(struct ceph_mds_request *req,
  1103. struct ceph_mds_session *session)
  1104. {
  1105. struct dentry *parent = req->r_dentry;
  1106. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  1107. struct qstr dname;
  1108. struct dentry *dn;
  1109. struct inode *in;
  1110. int err = 0, ret, i;
  1111. struct inode *snapdir = NULL;
  1112. struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
  1113. struct ceph_dentry_info *di;
  1114. u64 r_readdir_offset = req->r_readdir_offset;
  1115. u32 frag = le32_to_cpu(rhead->args.readdir.frag);
  1116. if (rinfo->dir_dir &&
  1117. le32_to_cpu(rinfo->dir_dir->frag) != frag) {
  1118. dout("readdir_prepopulate got new frag %x -> %x\n",
  1119. frag, le32_to_cpu(rinfo->dir_dir->frag));
  1120. frag = le32_to_cpu(rinfo->dir_dir->frag);
  1121. if (ceph_frag_is_leftmost(frag))
  1122. r_readdir_offset = 2;
  1123. else
  1124. r_readdir_offset = 0;
  1125. }
  1126. if (req->r_aborted)
  1127. return readdir_prepopulate_inodes_only(req, session);
  1128. if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
  1129. snapdir = ceph_get_snapdir(parent->d_inode);
  1130. parent = d_find_alias(snapdir);
  1131. dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
  1132. rinfo->dir_nr, parent);
  1133. } else {
  1134. dout("readdir_prepopulate %d items under dn %p\n",
  1135. rinfo->dir_nr, parent);
  1136. if (rinfo->dir_dir)
  1137. ceph_fill_dirfrag(parent->d_inode, rinfo->dir_dir);
  1138. }
  1139. /* FIXME: release caps/leases if error occurs */
  1140. for (i = 0; i < rinfo->dir_nr; i++) {
  1141. struct ceph_vino vino;
  1142. dname.name = rinfo->dir_dname[i];
  1143. dname.len = rinfo->dir_dname_len[i];
  1144. dname.hash = full_name_hash(dname.name, dname.len);
  1145. vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
  1146. vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
  1147. retry_lookup:
  1148. dn = d_lookup(parent, &dname);
  1149. dout("d_lookup on parent=%p name=%.*s got %p\n",
  1150. parent, dname.len, dname.name, dn);
  1151. if (!dn) {
  1152. dn = d_alloc(parent, &dname);
  1153. dout("d_alloc %p '%.*s' = %p\n", parent,
  1154. dname.len, dname.name, dn);
  1155. if (dn == NULL) {
  1156. dout("d_alloc badness\n");
  1157. err = -ENOMEM;
  1158. goto out;
  1159. }
  1160. ret = ceph_init_dentry(dn);
  1161. if (ret < 0) {
  1162. dput(dn);
  1163. err = ret;
  1164. goto out;
  1165. }
  1166. } else if (dn->d_inode &&
  1167. (ceph_ino(dn->d_inode) != vino.ino ||
  1168. ceph_snap(dn->d_inode) != vino.snap)) {
  1169. dout(" dn %p points to wrong inode %p\n",
  1170. dn, dn->d_inode);
  1171. d_delete(dn);
  1172. dput(dn);
  1173. goto retry_lookup;
  1174. } else {
  1175. /* reorder parent's d_subdirs */
  1176. spin_lock(&parent->d_lock);
  1177. spin_lock_nested(&dn->d_lock, DENTRY_D_LOCK_NESTED);
  1178. list_move(&dn->d_u.d_child, &parent->d_subdirs);
  1179. spin_unlock(&dn->d_lock);
  1180. spin_unlock(&parent->d_lock);
  1181. }
  1182. /* inode */
  1183. if (dn->d_inode) {
  1184. in = dn->d_inode;
  1185. } else {
  1186. in = ceph_get_inode(parent->d_sb, vino);
  1187. if (IS_ERR(in)) {
  1188. dout("new_inode badness\n");
  1189. d_drop(dn);
  1190. dput(dn);
  1191. err = PTR_ERR(in);
  1192. goto out;
  1193. }
  1194. }
  1195. if (fill_inode(in, &rinfo->dir_in[i], NULL, session,
  1196. req->r_request_started, -1,
  1197. &req->r_caps_reservation) < 0) {
  1198. pr_err("fill_inode badness on %p\n", in);
  1199. if (!dn->d_inode)
  1200. iput(in);
  1201. d_drop(dn);
  1202. goto next_item;
  1203. }
  1204. if (!dn->d_inode) {
  1205. dn = splice_dentry(dn, in, NULL, false);
  1206. if (IS_ERR(dn)) {
  1207. err = PTR_ERR(dn);
  1208. dn = NULL;
  1209. goto next_item;
  1210. }
  1211. }
  1212. di = dn->d_fsdata;
  1213. di->offset = ceph_make_fpos(frag, i + r_readdir_offset);
  1214. update_dentry_lease(dn, rinfo->dir_dlease[i],
  1215. req->r_session,
  1216. req->r_request_started);
  1217. next_item:
  1218. if (dn)
  1219. dput(dn);
  1220. }
  1221. if (err == 0)
  1222. req->r_did_prepopulate = true;
  1223. out:
  1224. if (snapdir) {
  1225. iput(snapdir);
  1226. dput(parent);
  1227. }
  1228. dout("readdir_prepopulate done\n");
  1229. return err;
  1230. }
  1231. int ceph_inode_set_size(struct inode *inode, loff_t size)
  1232. {
  1233. struct ceph_inode_info *ci = ceph_inode(inode);
  1234. int ret = 0;
  1235. spin_lock(&ci->i_ceph_lock);
  1236. dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
  1237. inode->i_size = size;
  1238. inode->i_blocks = (size + (1 << 9) - 1) >> 9;
  1239. /* tell the MDS if we are approaching max_size */
  1240. if ((size << 1) >= ci->i_max_size &&
  1241. (ci->i_reported_size << 1) < ci->i_max_size)
  1242. ret = 1;
  1243. spin_unlock(&ci->i_ceph_lock);
  1244. return ret;
  1245. }
  1246. /*
  1247. * Write back inode data in a worker thread. (This can't be done
  1248. * in the message handler context.)
  1249. */
  1250. void ceph_queue_writeback(struct inode *inode)
  1251. {
  1252. ihold(inode);
  1253. if (queue_work(ceph_inode_to_client(inode)->wb_wq,
  1254. &ceph_inode(inode)->i_wb_work)) {
  1255. dout("ceph_queue_writeback %p\n", inode);
  1256. } else {
  1257. dout("ceph_queue_writeback %p failed\n", inode);
  1258. iput(inode);
  1259. }
  1260. }
  1261. static void ceph_writeback_work(struct work_struct *work)
  1262. {
  1263. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1264. i_wb_work);
  1265. struct inode *inode = &ci->vfs_inode;
  1266. dout("writeback %p\n", inode);
  1267. filemap_fdatawrite(&inode->i_data);
  1268. iput(inode);
  1269. }
  1270. /*
  1271. * queue an async invalidation
  1272. */
  1273. void ceph_queue_invalidate(struct inode *inode)
  1274. {
  1275. ihold(inode);
  1276. if (queue_work(ceph_inode_to_client(inode)->pg_inv_wq,
  1277. &ceph_inode(inode)->i_pg_inv_work)) {
  1278. dout("ceph_queue_invalidate %p\n", inode);
  1279. } else {
  1280. dout("ceph_queue_invalidate %p failed\n", inode);
  1281. iput(inode);
  1282. }
  1283. }
  1284. /*
  1285. * Invalidate inode pages in a worker thread. (This can't be done
  1286. * in the message handler context.)
  1287. */
  1288. static void ceph_invalidate_work(struct work_struct *work)
  1289. {
  1290. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1291. i_pg_inv_work);
  1292. struct inode *inode = &ci->vfs_inode;
  1293. u32 orig_gen;
  1294. int check = 0;
  1295. mutex_lock(&ci->i_truncate_mutex);
  1296. spin_lock(&ci->i_ceph_lock);
  1297. dout("invalidate_pages %p gen %d revoking %d\n", inode,
  1298. ci->i_rdcache_gen, ci->i_rdcache_revoking);
  1299. if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
  1300. if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
  1301. check = 1;
  1302. spin_unlock(&ci->i_ceph_lock);
  1303. mutex_unlock(&ci->i_truncate_mutex);
  1304. goto out;
  1305. }
  1306. orig_gen = ci->i_rdcache_gen;
  1307. spin_unlock(&ci->i_ceph_lock);
  1308. truncate_inode_pages(inode->i_mapping, 0);
  1309. spin_lock(&ci->i_ceph_lock);
  1310. if (orig_gen == ci->i_rdcache_gen &&
  1311. orig_gen == ci->i_rdcache_revoking) {
  1312. dout("invalidate_pages %p gen %d successful\n", inode,
  1313. ci->i_rdcache_gen);
  1314. ci->i_rdcache_revoking--;
  1315. check = 1;
  1316. } else {
  1317. dout("invalidate_pages %p gen %d raced, now %d revoking %d\n",
  1318. inode, orig_gen, ci->i_rdcache_gen,
  1319. ci->i_rdcache_revoking);
  1320. if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
  1321. check = 1;
  1322. }
  1323. spin_unlock(&ci->i_ceph_lock);
  1324. mutex_unlock(&ci->i_truncate_mutex);
  1325. out:
  1326. if (check)
  1327. ceph_check_caps(ci, 0, NULL);
  1328. iput(inode);
  1329. }
  1330. /*
  1331. * called by trunc_wq;
  1332. *
  1333. * We also truncate in a separate thread as well.
  1334. */
  1335. static void ceph_vmtruncate_work(struct work_struct *work)
  1336. {
  1337. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1338. i_vmtruncate_work);
  1339. struct inode *inode = &ci->vfs_inode;
  1340. dout("vmtruncate_work %p\n", inode);
  1341. __ceph_do_pending_vmtruncate(inode);
  1342. iput(inode);
  1343. }
  1344. /*
  1345. * Queue an async vmtruncate. If we fail to queue work, we will handle
  1346. * the truncation the next time we call __ceph_do_pending_vmtruncate.
  1347. */
  1348. void ceph_queue_vmtruncate(struct inode *inode)
  1349. {
  1350. struct ceph_inode_info *ci = ceph_inode(inode);
  1351. ihold(inode);
  1352. if (queue_work(ceph_sb_to_client(inode->i_sb)->trunc_wq,
  1353. &ci->i_vmtruncate_work)) {
  1354. dout("ceph_queue_vmtruncate %p\n", inode);
  1355. } else {
  1356. dout("ceph_queue_vmtruncate %p failed, pending=%d\n",
  1357. inode, ci->i_truncate_pending);
  1358. iput(inode);
  1359. }
  1360. }
  1361. /*
  1362. * Make sure any pending truncation is applied before doing anything
  1363. * that may depend on it.
  1364. */
  1365. void __ceph_do_pending_vmtruncate(struct inode *inode)
  1366. {
  1367. struct ceph_inode_info *ci = ceph_inode(inode);
  1368. u64 to;
  1369. int wrbuffer_refs, finish = 0;
  1370. mutex_lock(&ci->i_truncate_mutex);
  1371. retry:
  1372. spin_lock(&ci->i_ceph_lock);
  1373. if (ci->i_truncate_pending == 0) {
  1374. dout("__do_pending_vmtruncate %p none pending\n", inode);
  1375. spin_unlock(&ci->i_ceph_lock);
  1376. mutex_unlock(&ci->i_truncate_mutex);
  1377. return;
  1378. }
  1379. /*
  1380. * make sure any dirty snapped pages are flushed before we
  1381. * possibly truncate them.. so write AND block!
  1382. */
  1383. if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
  1384. dout("__do_pending_vmtruncate %p flushing snaps first\n",
  1385. inode);
  1386. spin_unlock(&ci->i_ceph_lock);
  1387. filemap_write_and_wait_range(&inode->i_data, 0,
  1388. inode->i_sb->s_maxbytes);
  1389. goto retry;
  1390. }
  1391. /* there should be no reader or writer */
  1392. WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref);
  1393. to = ci->i_truncate_size;
  1394. wrbuffer_refs = ci->i_wrbuffer_ref;
  1395. dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
  1396. ci->i_truncate_pending, to);
  1397. spin_unlock(&ci->i_ceph_lock);
  1398. truncate_inode_pages(inode->i_mapping, to);
  1399. spin_lock(&ci->i_ceph_lock);
  1400. if (to == ci->i_truncate_size) {
  1401. ci->i_truncate_pending = 0;
  1402. finish = 1;
  1403. }
  1404. spin_unlock(&ci->i_ceph_lock);
  1405. if (!finish)
  1406. goto retry;
  1407. mutex_unlock(&ci->i_truncate_mutex);
  1408. if (wrbuffer_refs == 0)
  1409. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  1410. wake_up_all(&ci->i_cap_wq);
  1411. }
  1412. /*
  1413. * symlinks
  1414. */
  1415. static void *ceph_sym_follow_link(struct dentry *dentry, struct nameidata *nd)
  1416. {
  1417. struct ceph_inode_info *ci = ceph_inode(dentry->d_inode);
  1418. nd_set_link(nd, ci->i_symlink);
  1419. return NULL;
  1420. }
  1421. static const struct inode_operations ceph_symlink_iops = {
  1422. .readlink = generic_readlink,
  1423. .follow_link = ceph_sym_follow_link,
  1424. .setattr = ceph_setattr,
  1425. .getattr = ceph_getattr,
  1426. .setxattr = ceph_setxattr,
  1427. .getxattr = ceph_getxattr,
  1428. .listxattr = ceph_listxattr,
  1429. .removexattr = ceph_removexattr,
  1430. .get_acl = ceph_get_acl,
  1431. .set_acl = ceph_set_acl,
  1432. };
  1433. /*
  1434. * setattr
  1435. */
  1436. int ceph_setattr(struct dentry *dentry, struct iattr *attr)
  1437. {
  1438. struct inode *inode = dentry->d_inode;
  1439. struct ceph_inode_info *ci = ceph_inode(inode);
  1440. struct inode *parent_inode;
  1441. const unsigned int ia_valid = attr->ia_valid;
  1442. struct ceph_mds_request *req;
  1443. struct ceph_mds_client *mdsc = ceph_sb_to_client(dentry->d_sb)->mdsc;
  1444. int issued;
  1445. int release = 0, dirtied = 0;
  1446. int mask = 0;
  1447. int err = 0;
  1448. int inode_dirty_flags = 0;
  1449. if (ceph_snap(inode) != CEPH_NOSNAP)
  1450. return -EROFS;
  1451. err = inode_change_ok(inode, attr);
  1452. if (err != 0)
  1453. return err;
  1454. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
  1455. USE_AUTH_MDS);
  1456. if (IS_ERR(req))
  1457. return PTR_ERR(req);
  1458. spin_lock(&ci->i_ceph_lock);
  1459. issued = __ceph_caps_issued(ci, NULL);
  1460. dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
  1461. if (ia_valid & ATTR_UID) {
  1462. dout("setattr %p uid %d -> %d\n", inode,
  1463. from_kuid(&init_user_ns, inode->i_uid),
  1464. from_kuid(&init_user_ns, attr->ia_uid));
  1465. if (issued & CEPH_CAP_AUTH_EXCL) {
  1466. inode->i_uid = attr->ia_uid;
  1467. dirtied |= CEPH_CAP_AUTH_EXCL;
  1468. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1469. !uid_eq(attr->ia_uid, inode->i_uid)) {
  1470. req->r_args.setattr.uid = cpu_to_le32(
  1471. from_kuid(&init_user_ns, attr->ia_uid));
  1472. mask |= CEPH_SETATTR_UID;
  1473. release |= CEPH_CAP_AUTH_SHARED;
  1474. }
  1475. }
  1476. if (ia_valid & ATTR_GID) {
  1477. dout("setattr %p gid %d -> %d\n", inode,
  1478. from_kgid(&init_user_ns, inode->i_gid),
  1479. from_kgid(&init_user_ns, attr->ia_gid));
  1480. if (issued & CEPH_CAP_AUTH_EXCL) {
  1481. inode->i_gid = attr->ia_gid;
  1482. dirtied |= CEPH_CAP_AUTH_EXCL;
  1483. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1484. !gid_eq(attr->ia_gid, inode->i_gid)) {
  1485. req->r_args.setattr.gid = cpu_to_le32(
  1486. from_kgid(&init_user_ns, attr->ia_gid));
  1487. mask |= CEPH_SETATTR_GID;
  1488. release |= CEPH_CAP_AUTH_SHARED;
  1489. }
  1490. }
  1491. if (ia_valid & ATTR_MODE) {
  1492. dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
  1493. attr->ia_mode);
  1494. if (issued & CEPH_CAP_AUTH_EXCL) {
  1495. inode->i_mode = attr->ia_mode;
  1496. dirtied |= CEPH_CAP_AUTH_EXCL;
  1497. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1498. attr->ia_mode != inode->i_mode) {
  1499. inode->i_mode = attr->ia_mode;
  1500. req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
  1501. mask |= CEPH_SETATTR_MODE;
  1502. release |= CEPH_CAP_AUTH_SHARED;
  1503. }
  1504. }
  1505. if (ia_valid & ATTR_ATIME) {
  1506. dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode,
  1507. inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
  1508. attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
  1509. if (issued & CEPH_CAP_FILE_EXCL) {
  1510. ci->i_time_warp_seq++;
  1511. inode->i_atime = attr->ia_atime;
  1512. dirtied |= CEPH_CAP_FILE_EXCL;
  1513. } else if ((issued & CEPH_CAP_FILE_WR) &&
  1514. timespec_compare(&inode->i_atime,
  1515. &attr->ia_atime) < 0) {
  1516. inode->i_atime = attr->ia_atime;
  1517. dirtied |= CEPH_CAP_FILE_WR;
  1518. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1519. !timespec_equal(&inode->i_atime, &attr->ia_atime)) {
  1520. ceph_encode_timespec(&req->r_args.setattr.atime,
  1521. &attr->ia_atime);
  1522. mask |= CEPH_SETATTR_ATIME;
  1523. release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD |
  1524. CEPH_CAP_FILE_WR;
  1525. }
  1526. }
  1527. if (ia_valid & ATTR_MTIME) {
  1528. dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode,
  1529. inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
  1530. attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
  1531. if (issued & CEPH_CAP_FILE_EXCL) {
  1532. ci->i_time_warp_seq++;
  1533. inode->i_mtime = attr->ia_mtime;
  1534. dirtied |= CEPH_CAP_FILE_EXCL;
  1535. } else if ((issued & CEPH_CAP_FILE_WR) &&
  1536. timespec_compare(&inode->i_mtime,
  1537. &attr->ia_mtime) < 0) {
  1538. inode->i_mtime = attr->ia_mtime;
  1539. dirtied |= CEPH_CAP_FILE_WR;
  1540. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1541. !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) {
  1542. ceph_encode_timespec(&req->r_args.setattr.mtime,
  1543. &attr->ia_mtime);
  1544. mask |= CEPH_SETATTR_MTIME;
  1545. release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
  1546. CEPH_CAP_FILE_WR;
  1547. }
  1548. }
  1549. if (ia_valid & ATTR_SIZE) {
  1550. dout("setattr %p size %lld -> %lld\n", inode,
  1551. inode->i_size, attr->ia_size);
  1552. if (attr->ia_size > inode->i_sb->s_maxbytes) {
  1553. err = -EINVAL;
  1554. goto out;
  1555. }
  1556. if ((issued & CEPH_CAP_FILE_EXCL) &&
  1557. attr->ia_size > inode->i_size) {
  1558. inode->i_size = attr->ia_size;
  1559. inode->i_blocks =
  1560. (attr->ia_size + (1 << 9) - 1) >> 9;
  1561. inode->i_ctime = attr->ia_ctime;
  1562. ci->i_reported_size = attr->ia_size;
  1563. dirtied |= CEPH_CAP_FILE_EXCL;
  1564. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1565. attr->ia_size != inode->i_size) {
  1566. req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
  1567. req->r_args.setattr.old_size =
  1568. cpu_to_le64(inode->i_size);
  1569. mask |= CEPH_SETATTR_SIZE;
  1570. release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
  1571. CEPH_CAP_FILE_WR;
  1572. }
  1573. }
  1574. /* these do nothing */
  1575. if (ia_valid & ATTR_CTIME) {
  1576. bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
  1577. ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
  1578. dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode,
  1579. inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
  1580. attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
  1581. only ? "ctime only" : "ignored");
  1582. inode->i_ctime = attr->ia_ctime;
  1583. if (only) {
  1584. /*
  1585. * if kernel wants to dirty ctime but nothing else,
  1586. * we need to choose a cap to dirty under, or do
  1587. * a almost-no-op setattr
  1588. */
  1589. if (issued & CEPH_CAP_AUTH_EXCL)
  1590. dirtied |= CEPH_CAP_AUTH_EXCL;
  1591. else if (issued & CEPH_CAP_FILE_EXCL)
  1592. dirtied |= CEPH_CAP_FILE_EXCL;
  1593. else if (issued & CEPH_CAP_XATTR_EXCL)
  1594. dirtied |= CEPH_CAP_XATTR_EXCL;
  1595. else
  1596. mask |= CEPH_SETATTR_CTIME;
  1597. }
  1598. }
  1599. if (ia_valid & ATTR_FILE)
  1600. dout("setattr %p ATTR_FILE ... hrm!\n", inode);
  1601. if (dirtied) {
  1602. inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied);
  1603. inode->i_ctime = CURRENT_TIME;
  1604. }
  1605. release &= issued;
  1606. spin_unlock(&ci->i_ceph_lock);
  1607. if (inode_dirty_flags)
  1608. __mark_inode_dirty(inode, inode_dirty_flags);
  1609. if (ia_valid & ATTR_MODE) {
  1610. err = posix_acl_chmod(inode, attr->ia_mode);
  1611. if (err)
  1612. goto out_put;
  1613. }
  1614. if (mask) {
  1615. req->r_inode = inode;
  1616. ihold(inode);
  1617. req->r_inode_drop = release;
  1618. req->r_args.setattr.mask = cpu_to_le32(mask);
  1619. req->r_num_caps = 1;
  1620. parent_inode = ceph_get_dentry_parent_inode(dentry);
  1621. err = ceph_mdsc_do_request(mdsc, parent_inode, req);
  1622. iput(parent_inode);
  1623. }
  1624. dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
  1625. ceph_cap_string(dirtied), mask);
  1626. ceph_mdsc_put_request(req);
  1627. if (mask & CEPH_SETATTR_SIZE)
  1628. __ceph_do_pending_vmtruncate(inode);
  1629. return err;
  1630. out:
  1631. spin_unlock(&ci->i_ceph_lock);
  1632. out_put:
  1633. ceph_mdsc_put_request(req);
  1634. return err;
  1635. }
  1636. /*
  1637. * Verify that we have a lease on the given mask. If not,
  1638. * do a getattr against an mds.
  1639. */
  1640. int ceph_do_getattr(struct inode *inode, int mask)
  1641. {
  1642. struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
  1643. struct ceph_mds_client *mdsc = fsc->mdsc;
  1644. struct ceph_mds_request *req;
  1645. int err;
  1646. if (ceph_snap(inode) == CEPH_SNAPDIR) {
  1647. dout("do_getattr inode %p SNAPDIR\n", inode);
  1648. return 0;
  1649. }
  1650. dout("do_getattr inode %p mask %s mode 0%o\n", inode, ceph_cap_string(mask), inode->i_mode);
  1651. if (ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
  1652. return 0;
  1653. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
  1654. if (IS_ERR(req))
  1655. return PTR_ERR(req);
  1656. req->r_inode = inode;
  1657. ihold(inode);
  1658. req->r_num_caps = 1;
  1659. req->r_args.getattr.mask = cpu_to_le32(mask);
  1660. err = ceph_mdsc_do_request(mdsc, NULL, req);
  1661. ceph_mdsc_put_request(req);
  1662. dout("do_getattr result=%d\n", err);
  1663. return err;
  1664. }
  1665. /*
  1666. * Check inode permissions. We verify we have a valid value for
  1667. * the AUTH cap, then call the generic handler.
  1668. */
  1669. int ceph_permission(struct inode *inode, int mask)
  1670. {
  1671. int err;
  1672. if (mask & MAY_NOT_BLOCK)
  1673. return -ECHILD;
  1674. err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED);
  1675. if (!err)
  1676. err = generic_permission(inode, mask);
  1677. return err;
  1678. }
  1679. /*
  1680. * Get all attributes. Hopefully somedata we'll have a statlite()
  1681. * and can limit the fields we require to be accurate.
  1682. */
  1683. int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1684. struct kstat *stat)
  1685. {
  1686. struct inode *inode = dentry->d_inode;
  1687. struct ceph_inode_info *ci = ceph_inode(inode);
  1688. int err;
  1689. err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL);
  1690. if (!err) {
  1691. generic_fillattr(inode, stat);
  1692. stat->ino = ceph_translate_ino(inode->i_sb, inode->i_ino);
  1693. if (ceph_snap(inode) != CEPH_NOSNAP)
  1694. stat->dev = ceph_snap(inode);
  1695. else
  1696. stat->dev = 0;
  1697. if (S_ISDIR(inode->i_mode)) {
  1698. if (ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb),
  1699. RBYTES))
  1700. stat->size = ci->i_rbytes;
  1701. else
  1702. stat->size = ci->i_files + ci->i_subdirs;
  1703. stat->blocks = 0;
  1704. stat->blksize = 65536;
  1705. }
  1706. }
  1707. return err;
  1708. }