inode.c 51 KB

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