inode.c 54 KB

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