inode.c 50 KB

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