inode.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/module.h>
  4. #include <linux/fs.h>
  5. #include <linux/slab.h>
  6. #include <linux/string.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/kernel.h>
  9. #include <linux/writeback.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/xattr.h>
  12. #include <linux/posix_acl.h>
  13. #include <linux/random.h>
  14. #include <linux/sort.h>
  15. #include <linux/iversion.h>
  16. #include <linux/fscrypt.h>
  17. #include "super.h"
  18. #include "mds_client.h"
  19. #include "cache.h"
  20. #include "crypto.h"
  21. #include <linux/ceph/decode.h>
  22. /*
  23. * Ceph inode operations
  24. *
  25. * Implement basic inode helpers (get, alloc) and inode ops (getattr,
  26. * setattr, etc.), xattr helpers, and helpers for assimilating
  27. * metadata returned by the MDS into our cache.
  28. *
  29. * Also define helpers for doing asynchronous writeback, invalidation,
  30. * and truncation for the benefit of those who can't afford to block
  31. * (typically because they are in the message handler path).
  32. */
  33. static const struct inode_operations ceph_symlink_iops;
  34. static const struct inode_operations ceph_encrypted_symlink_iops;
  35. static void ceph_inode_work(struct work_struct *work);
  36. /*
  37. * find or create an inode, given the ceph ino number
  38. */
  39. static int ceph_set_ino_cb(struct inode *inode, void *data)
  40. {
  41. struct ceph_inode_info *ci = ceph_inode(inode);
  42. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
  43. ci->i_vino = *(struct ceph_vino *)data;
  44. inode->i_ino = ceph_vino_to_ino_t(ci->i_vino);
  45. inode_set_iversion_raw(inode, 0);
  46. percpu_counter_inc(&mdsc->metric.total_inodes);
  47. return 0;
  48. }
  49. /*
  50. * Check if the parent inode matches the vino from directory reply info
  51. */
  52. static inline bool ceph_vino_matches_parent(struct inode *parent,
  53. struct ceph_vino vino)
  54. {
  55. return ceph_ino(parent) == vino.ino && ceph_snap(parent) == vino.snap;
  56. }
  57. /*
  58. * Validate that the directory inode referenced by @req->r_parent matches the
  59. * inode number and snapshot id contained in the reply's directory record. If
  60. * they do not match – which can theoretically happen if the parent dentry was
  61. * moved between the time the request was issued and the reply arrived – fall
  62. * back to looking up the correct inode in the inode cache.
  63. *
  64. * A reference is *always* returned. Callers that receive a different inode
  65. * than the original @parent are responsible for dropping the extra reference
  66. * once the reply has been processed.
  67. */
  68. static struct inode *ceph_get_reply_dir(struct super_block *sb,
  69. struct inode *parent,
  70. struct ceph_mds_reply_info_parsed *rinfo)
  71. {
  72. struct ceph_vino vino;
  73. if (unlikely(!rinfo->diri.in))
  74. return parent; /* nothing to compare against */
  75. /* If we didn't have a cached parent inode to begin with, just bail out. */
  76. if (!parent)
  77. return NULL;
  78. vino.ino = le64_to_cpu(rinfo->diri.in->ino);
  79. vino.snap = le64_to_cpu(rinfo->diri.in->snapid);
  80. if (likely(ceph_vino_matches_parent(parent, vino)))
  81. return parent; /* matches – use the original reference */
  82. /* Mismatch – this should be rare. Emit a WARN and obtain the correct inode. */
  83. WARN_ONCE(1, "ceph: reply dir mismatch (parent valid %llx.%llx reply %llx.%llx)\n",
  84. ceph_ino(parent), ceph_snap(parent), vino.ino, vino.snap);
  85. return ceph_get_inode(sb, vino, NULL);
  86. }
  87. /**
  88. * ceph_new_inode - allocate a new inode in advance of an expected create
  89. * @dir: parent directory for new inode
  90. * @dentry: dentry that may eventually point to new inode
  91. * @mode: mode of new inode
  92. * @as_ctx: pointer to inherited security context
  93. *
  94. * Allocate a new inode in advance of an operation to create a new inode.
  95. * This allocates the inode and sets up the acl_sec_ctx with appropriate
  96. * info for the new inode.
  97. *
  98. * Returns a pointer to the new inode or an ERR_PTR.
  99. */
  100. struct inode *ceph_new_inode(struct inode *dir, struct dentry *dentry,
  101. umode_t *mode, struct ceph_acl_sec_ctx *as_ctx)
  102. {
  103. int err;
  104. struct inode *inode;
  105. inode = new_inode(dir->i_sb);
  106. if (!inode)
  107. return ERR_PTR(-ENOMEM);
  108. inode->i_blkbits = CEPH_FSCRYPT_BLOCK_SHIFT;
  109. if (!S_ISLNK(*mode)) {
  110. err = ceph_pre_init_acls(dir, mode, as_ctx);
  111. if (err < 0)
  112. goto out_err;
  113. }
  114. inode->i_state = 0;
  115. inode->i_mode = *mode;
  116. err = ceph_security_init_secctx(dentry, *mode, as_ctx);
  117. if (err < 0)
  118. goto out_err;
  119. /*
  120. * We'll skip setting fscrypt context for snapshots, leaving that for
  121. * the handle_reply().
  122. */
  123. if (ceph_snap(dir) != CEPH_SNAPDIR) {
  124. err = ceph_fscrypt_prepare_context(dir, inode, as_ctx);
  125. if (err)
  126. goto out_err;
  127. }
  128. return inode;
  129. out_err:
  130. iput(inode);
  131. return ERR_PTR(err);
  132. }
  133. void ceph_as_ctx_to_req(struct ceph_mds_request *req,
  134. struct ceph_acl_sec_ctx *as_ctx)
  135. {
  136. if (as_ctx->pagelist) {
  137. req->r_pagelist = as_ctx->pagelist;
  138. as_ctx->pagelist = NULL;
  139. }
  140. ceph_fscrypt_as_ctx_to_req(req, as_ctx);
  141. }
  142. /**
  143. * ceph_get_inode - find or create/hash a new inode
  144. * @sb: superblock to search and allocate in
  145. * @vino: vino to search for
  146. * @newino: optional new inode to insert if one isn't found (may be NULL)
  147. *
  148. * Search for or insert a new inode into the hash for the given vino, and
  149. * return a reference to it. If new is non-NULL, its reference is consumed.
  150. */
  151. struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino,
  152. struct inode *newino)
  153. {
  154. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(sb);
  155. struct ceph_client *cl = mdsc->fsc->client;
  156. struct inode *inode;
  157. if (ceph_vino_is_reserved(vino))
  158. return ERR_PTR(-EREMOTEIO);
  159. if (newino) {
  160. inode = inode_insert5(newino, (unsigned long)vino.ino,
  161. ceph_ino_compare, ceph_set_ino_cb, &vino);
  162. if (inode != newino)
  163. iput(newino);
  164. } else {
  165. inode = iget5_locked(sb, (unsigned long)vino.ino,
  166. ceph_ino_compare, ceph_set_ino_cb, &vino);
  167. }
  168. if (!inode) {
  169. doutc(cl, "no inode found for %llx.%llx\n", vino.ino, vino.snap);
  170. return ERR_PTR(-ENOMEM);
  171. }
  172. doutc(cl, "on %llx=%llx.%llx got %p new %d\n",
  173. ceph_present_inode(inode), ceph_vinop(inode), inode,
  174. !!(inode->i_state & I_NEW));
  175. return inode;
  176. }
  177. /*
  178. * get/constuct snapdir inode for a given directory
  179. */
  180. struct inode *ceph_get_snapdir(struct inode *parent)
  181. {
  182. struct ceph_client *cl = ceph_inode_to_client(parent);
  183. struct ceph_vino vino = {
  184. .ino = ceph_ino(parent),
  185. .snap = CEPH_SNAPDIR,
  186. };
  187. struct inode *inode = ceph_get_inode(parent->i_sb, vino, NULL);
  188. struct ceph_inode_info *ci = ceph_inode(inode);
  189. int ret = -ENOTDIR;
  190. if (IS_ERR(inode))
  191. return inode;
  192. if (!S_ISDIR(parent->i_mode)) {
  193. pr_warn_once_client(cl, "bad snapdir parent type (mode=0%o)\n",
  194. parent->i_mode);
  195. goto err;
  196. }
  197. if (!(inode->i_state & I_NEW) && !S_ISDIR(inode->i_mode)) {
  198. pr_warn_once_client(cl, "bad snapdir inode type (mode=0%o)\n",
  199. inode->i_mode);
  200. goto err;
  201. }
  202. inode->i_mode = parent->i_mode;
  203. inode->i_uid = parent->i_uid;
  204. inode->i_gid = parent->i_gid;
  205. inode_set_mtime_to_ts(inode, inode_get_mtime(parent));
  206. inode_set_ctime_to_ts(inode, inode_get_ctime(parent));
  207. inode_set_atime_to_ts(inode, inode_get_atime(parent));
  208. ci->i_rbytes = 0;
  209. ci->i_btime = ceph_inode(parent)->i_btime;
  210. #ifdef CONFIG_FS_ENCRYPTION
  211. /* if encrypted, just borrow fscrypt_auth from parent */
  212. if (IS_ENCRYPTED(parent)) {
  213. struct ceph_inode_info *pci = ceph_inode(parent);
  214. ci->fscrypt_auth = kmemdup(pci->fscrypt_auth,
  215. pci->fscrypt_auth_len,
  216. GFP_KERNEL);
  217. if (ci->fscrypt_auth) {
  218. inode->i_flags |= S_ENCRYPTED;
  219. ci->fscrypt_auth_len = pci->fscrypt_auth_len;
  220. } else {
  221. doutc(cl, "Failed to alloc snapdir fscrypt_auth\n");
  222. ret = -ENOMEM;
  223. goto err;
  224. }
  225. }
  226. #endif
  227. if (inode->i_state & I_NEW) {
  228. inode->i_op = &ceph_snapdir_iops;
  229. inode->i_fop = &ceph_snapdir_fops;
  230. ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
  231. unlock_new_inode(inode);
  232. }
  233. return inode;
  234. err:
  235. if ((inode->i_state & I_NEW))
  236. discard_new_inode(inode);
  237. else
  238. iput(inode);
  239. return ERR_PTR(ret);
  240. }
  241. const struct inode_operations ceph_file_iops = {
  242. .permission = ceph_permission,
  243. .setattr = ceph_setattr,
  244. .getattr = ceph_getattr,
  245. .listxattr = ceph_listxattr,
  246. .get_inode_acl = ceph_get_acl,
  247. .set_acl = ceph_set_acl,
  248. };
  249. /*
  250. * We use a 'frag tree' to keep track of the MDS's directory fragments
  251. * for a given inode (usually there is just a single fragment). We
  252. * need to know when a child frag is delegated to a new MDS, or when
  253. * it is flagged as replicated, so we can direct our requests
  254. * accordingly.
  255. */
  256. /*
  257. * find/create a frag in the tree
  258. */
  259. static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
  260. u32 f)
  261. {
  262. struct inode *inode = &ci->netfs.inode;
  263. struct ceph_client *cl = ceph_inode_to_client(inode);
  264. struct rb_node **p;
  265. struct rb_node *parent = NULL;
  266. struct ceph_inode_frag *frag;
  267. int c;
  268. p = &ci->i_fragtree.rb_node;
  269. while (*p) {
  270. parent = *p;
  271. frag = rb_entry(parent, struct ceph_inode_frag, node);
  272. c = ceph_frag_compare(f, frag->frag);
  273. if (c < 0)
  274. p = &(*p)->rb_left;
  275. else if (c > 0)
  276. p = &(*p)->rb_right;
  277. else
  278. return frag;
  279. }
  280. frag = kmalloc(sizeof(*frag), GFP_NOFS);
  281. if (!frag)
  282. return ERR_PTR(-ENOMEM);
  283. frag->frag = f;
  284. frag->split_by = 0;
  285. frag->mds = -1;
  286. frag->ndist = 0;
  287. rb_link_node(&frag->node, parent, p);
  288. rb_insert_color(&frag->node, &ci->i_fragtree);
  289. doutc(cl, "added %p %llx.%llx frag %x\n", inode, ceph_vinop(inode), f);
  290. return frag;
  291. }
  292. /*
  293. * find a specific frag @f
  294. */
  295. struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
  296. {
  297. struct rb_node *n = ci->i_fragtree.rb_node;
  298. while (n) {
  299. struct ceph_inode_frag *frag =
  300. rb_entry(n, struct ceph_inode_frag, node);
  301. int c = ceph_frag_compare(f, frag->frag);
  302. if (c < 0)
  303. n = n->rb_left;
  304. else if (c > 0)
  305. n = n->rb_right;
  306. else
  307. return frag;
  308. }
  309. return NULL;
  310. }
  311. /*
  312. * Choose frag containing the given value @v. If @pfrag is
  313. * specified, copy the frag delegation info to the caller if
  314. * it is present.
  315. */
  316. static u32 __ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
  317. struct ceph_inode_frag *pfrag, int *found)
  318. {
  319. struct ceph_client *cl = ceph_inode_to_client(&ci->netfs.inode);
  320. u32 t = ceph_frag_make(0, 0);
  321. struct ceph_inode_frag *frag;
  322. unsigned nway, i;
  323. u32 n;
  324. if (found)
  325. *found = 0;
  326. while (1) {
  327. WARN_ON(!ceph_frag_contains_value(t, v));
  328. frag = __ceph_find_frag(ci, t);
  329. if (!frag)
  330. break; /* t is a leaf */
  331. if (frag->split_by == 0) {
  332. if (pfrag)
  333. memcpy(pfrag, frag, sizeof(*pfrag));
  334. if (found)
  335. *found = 1;
  336. break;
  337. }
  338. /* choose child */
  339. nway = 1 << frag->split_by;
  340. doutc(cl, "frag(%x) %x splits by %d (%d ways)\n", v, t,
  341. frag->split_by, nway);
  342. for (i = 0; i < nway; i++) {
  343. n = ceph_frag_make_child(t, frag->split_by, i);
  344. if (ceph_frag_contains_value(n, v)) {
  345. t = n;
  346. break;
  347. }
  348. }
  349. BUG_ON(i == nway);
  350. }
  351. doutc(cl, "frag(%x) = %x\n", v, t);
  352. return t;
  353. }
  354. u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
  355. struct ceph_inode_frag *pfrag, int *found)
  356. {
  357. u32 ret;
  358. mutex_lock(&ci->i_fragtree_mutex);
  359. ret = __ceph_choose_frag(ci, v, pfrag, found);
  360. mutex_unlock(&ci->i_fragtree_mutex);
  361. return ret;
  362. }
  363. /*
  364. * Process dirfrag (delegation) info from the mds. Include leaf
  365. * fragment in tree ONLY if ndist > 0. Otherwise, only
  366. * branches/splits are included in i_fragtree)
  367. */
  368. static int ceph_fill_dirfrag(struct inode *inode,
  369. struct ceph_mds_reply_dirfrag *dirinfo)
  370. {
  371. struct ceph_inode_info *ci = ceph_inode(inode);
  372. struct ceph_client *cl = ceph_inode_to_client(inode);
  373. struct ceph_inode_frag *frag;
  374. u32 id = le32_to_cpu(dirinfo->frag);
  375. int mds = le32_to_cpu(dirinfo->auth);
  376. int ndist = le32_to_cpu(dirinfo->ndist);
  377. int diri_auth = -1;
  378. int i;
  379. int err = 0;
  380. spin_lock(&ci->i_ceph_lock);
  381. if (ci->i_auth_cap)
  382. diri_auth = ci->i_auth_cap->mds;
  383. spin_unlock(&ci->i_ceph_lock);
  384. if (mds == -1) /* CDIR_AUTH_PARENT */
  385. mds = diri_auth;
  386. mutex_lock(&ci->i_fragtree_mutex);
  387. if (ndist == 0 && mds == diri_auth) {
  388. /* no delegation info needed. */
  389. frag = __ceph_find_frag(ci, id);
  390. if (!frag)
  391. goto out;
  392. if (frag->split_by == 0) {
  393. /* tree leaf, remove */
  394. doutc(cl, "removed %p %llx.%llx frag %x (no ref)\n",
  395. inode, ceph_vinop(inode), id);
  396. rb_erase(&frag->node, &ci->i_fragtree);
  397. kfree(frag);
  398. } else {
  399. /* tree branch, keep and clear */
  400. doutc(cl, "cleared %p %llx.%llx frag %x referral\n",
  401. inode, ceph_vinop(inode), id);
  402. frag->mds = -1;
  403. frag->ndist = 0;
  404. }
  405. goto out;
  406. }
  407. /* find/add this frag to store mds delegation info */
  408. frag = __get_or_create_frag(ci, id);
  409. if (IS_ERR(frag)) {
  410. /* this is not the end of the world; we can continue
  411. with bad/inaccurate delegation info */
  412. pr_err_client(cl, "ENOMEM on mds ref %p %llx.%llx fg %x\n",
  413. inode, ceph_vinop(inode),
  414. le32_to_cpu(dirinfo->frag));
  415. err = -ENOMEM;
  416. goto out;
  417. }
  418. frag->mds = mds;
  419. frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
  420. for (i = 0; i < frag->ndist; i++)
  421. frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
  422. doutc(cl, "%p %llx.%llx frag %x ndist=%d\n", inode,
  423. ceph_vinop(inode), frag->frag, frag->ndist);
  424. out:
  425. mutex_unlock(&ci->i_fragtree_mutex);
  426. return err;
  427. }
  428. static int frag_tree_split_cmp(const void *l, const void *r)
  429. {
  430. struct ceph_frag_tree_split *ls = (struct ceph_frag_tree_split*)l;
  431. struct ceph_frag_tree_split *rs = (struct ceph_frag_tree_split*)r;
  432. return ceph_frag_compare(le32_to_cpu(ls->frag),
  433. le32_to_cpu(rs->frag));
  434. }
  435. static bool is_frag_child(u32 f, struct ceph_inode_frag *frag)
  436. {
  437. if (!frag)
  438. return f == ceph_frag_make(0, 0);
  439. if (ceph_frag_bits(f) != ceph_frag_bits(frag->frag) + frag->split_by)
  440. return false;
  441. return ceph_frag_contains_value(frag->frag, ceph_frag_value(f));
  442. }
  443. static int ceph_fill_fragtree(struct inode *inode,
  444. struct ceph_frag_tree_head *fragtree,
  445. struct ceph_mds_reply_dirfrag *dirinfo)
  446. {
  447. struct ceph_client *cl = ceph_inode_to_client(inode);
  448. struct ceph_inode_info *ci = ceph_inode(inode);
  449. struct ceph_inode_frag *frag, *prev_frag = NULL;
  450. struct rb_node *rb_node;
  451. unsigned i, split_by, nsplits;
  452. u32 id;
  453. bool update = false;
  454. mutex_lock(&ci->i_fragtree_mutex);
  455. nsplits = le32_to_cpu(fragtree->nsplits);
  456. if (nsplits != ci->i_fragtree_nsplits) {
  457. update = true;
  458. } else if (nsplits) {
  459. i = get_random_u32_below(nsplits);
  460. id = le32_to_cpu(fragtree->splits[i].frag);
  461. if (!__ceph_find_frag(ci, id))
  462. update = true;
  463. } else if (!RB_EMPTY_ROOT(&ci->i_fragtree)) {
  464. rb_node = rb_first(&ci->i_fragtree);
  465. frag = rb_entry(rb_node, struct ceph_inode_frag, node);
  466. if (frag->frag != ceph_frag_make(0, 0) || rb_next(rb_node))
  467. update = true;
  468. }
  469. if (!update && dirinfo) {
  470. id = le32_to_cpu(dirinfo->frag);
  471. if (id != __ceph_choose_frag(ci, id, NULL, NULL))
  472. update = true;
  473. }
  474. if (!update)
  475. goto out_unlock;
  476. if (nsplits > 1) {
  477. sort(fragtree->splits, nsplits, sizeof(fragtree->splits[0]),
  478. frag_tree_split_cmp, NULL);
  479. }
  480. doutc(cl, "%p %llx.%llx\n", inode, ceph_vinop(inode));
  481. rb_node = rb_first(&ci->i_fragtree);
  482. for (i = 0; i < nsplits; i++) {
  483. id = le32_to_cpu(fragtree->splits[i].frag);
  484. split_by = le32_to_cpu(fragtree->splits[i].by);
  485. if (split_by == 0 || ceph_frag_bits(id) + split_by > 24) {
  486. pr_err_client(cl, "%p %llx.%llx invalid split %d/%u, "
  487. "frag %x split by %d\n", inode,
  488. ceph_vinop(inode), i, nsplits, id, split_by);
  489. continue;
  490. }
  491. frag = NULL;
  492. while (rb_node) {
  493. frag = rb_entry(rb_node, struct ceph_inode_frag, node);
  494. if (ceph_frag_compare(frag->frag, id) >= 0) {
  495. if (frag->frag != id)
  496. frag = NULL;
  497. else
  498. rb_node = rb_next(rb_node);
  499. break;
  500. }
  501. rb_node = rb_next(rb_node);
  502. /* delete stale split/leaf node */
  503. if (frag->split_by > 0 ||
  504. !is_frag_child(frag->frag, prev_frag)) {
  505. rb_erase(&frag->node, &ci->i_fragtree);
  506. if (frag->split_by > 0)
  507. ci->i_fragtree_nsplits--;
  508. kfree(frag);
  509. }
  510. frag = NULL;
  511. }
  512. if (!frag) {
  513. frag = __get_or_create_frag(ci, id);
  514. if (IS_ERR(frag))
  515. continue;
  516. }
  517. if (frag->split_by == 0)
  518. ci->i_fragtree_nsplits++;
  519. frag->split_by = split_by;
  520. doutc(cl, " frag %x split by %d\n", frag->frag, frag->split_by);
  521. prev_frag = frag;
  522. }
  523. while (rb_node) {
  524. frag = rb_entry(rb_node, struct ceph_inode_frag, node);
  525. rb_node = rb_next(rb_node);
  526. /* delete stale split/leaf node */
  527. if (frag->split_by > 0 ||
  528. !is_frag_child(frag->frag, prev_frag)) {
  529. rb_erase(&frag->node, &ci->i_fragtree);
  530. if (frag->split_by > 0)
  531. ci->i_fragtree_nsplits--;
  532. kfree(frag);
  533. }
  534. }
  535. out_unlock:
  536. mutex_unlock(&ci->i_fragtree_mutex);
  537. return 0;
  538. }
  539. /*
  540. * initialize a newly allocated inode.
  541. */
  542. struct inode *ceph_alloc_inode(struct super_block *sb)
  543. {
  544. struct ceph_fs_client *fsc = ceph_sb_to_fs_client(sb);
  545. struct ceph_inode_info *ci;
  546. int i;
  547. ci = alloc_inode_sb(sb, ceph_inode_cachep, GFP_NOFS);
  548. if (!ci)
  549. return NULL;
  550. doutc(fsc->client, "%p\n", &ci->netfs.inode);
  551. /* Set parameters for the netfs library */
  552. netfs_inode_init(&ci->netfs, &ceph_netfs_ops, false);
  553. spin_lock_init(&ci->i_ceph_lock);
  554. ci->i_version = 0;
  555. ci->i_inline_version = 0;
  556. ci->i_time_warp_seq = 0;
  557. ci->i_ceph_flags = 0;
  558. atomic64_set(&ci->i_ordered_count, 1);
  559. atomic64_set(&ci->i_release_count, 1);
  560. atomic64_set(&ci->i_complete_seq[0], 0);
  561. atomic64_set(&ci->i_complete_seq[1], 0);
  562. ci->i_symlink = NULL;
  563. ci->i_max_bytes = 0;
  564. ci->i_max_files = 0;
  565. memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout));
  566. memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout));
  567. RCU_INIT_POINTER(ci->i_layout.pool_ns, NULL);
  568. ci->i_fragtree = RB_ROOT;
  569. mutex_init(&ci->i_fragtree_mutex);
  570. ci->i_xattrs.blob = NULL;
  571. ci->i_xattrs.prealloc_blob = NULL;
  572. ci->i_xattrs.dirty = false;
  573. ci->i_xattrs.index = RB_ROOT;
  574. ci->i_xattrs.count = 0;
  575. ci->i_xattrs.names_size = 0;
  576. ci->i_xattrs.vals_size = 0;
  577. ci->i_xattrs.version = 0;
  578. ci->i_xattrs.index_version = 0;
  579. ci->i_caps = RB_ROOT;
  580. ci->i_auth_cap = NULL;
  581. ci->i_dirty_caps = 0;
  582. ci->i_flushing_caps = 0;
  583. INIT_LIST_HEAD(&ci->i_dirty_item);
  584. INIT_LIST_HEAD(&ci->i_flushing_item);
  585. ci->i_prealloc_cap_flush = NULL;
  586. INIT_LIST_HEAD(&ci->i_cap_flush_list);
  587. init_waitqueue_head(&ci->i_cap_wq);
  588. ci->i_hold_caps_max = 0;
  589. INIT_LIST_HEAD(&ci->i_cap_delay_list);
  590. INIT_LIST_HEAD(&ci->i_cap_snaps);
  591. ci->i_head_snapc = NULL;
  592. ci->i_snap_caps = 0;
  593. ci->i_last_rd = ci->i_last_wr = jiffies - 3600 * HZ;
  594. for (i = 0; i < CEPH_FILE_MODE_BITS; i++)
  595. ci->i_nr_by_mode[i] = 0;
  596. mutex_init(&ci->i_truncate_mutex);
  597. ci->i_truncate_seq = 0;
  598. ci->i_truncate_size = 0;
  599. ci->i_truncate_pending = 0;
  600. ci->i_truncate_pagecache_size = 0;
  601. ci->i_max_size = 0;
  602. ci->i_reported_size = 0;
  603. ci->i_wanted_max_size = 0;
  604. ci->i_requested_max_size = 0;
  605. ci->i_pin_ref = 0;
  606. ci->i_rd_ref = 0;
  607. ci->i_rdcache_ref = 0;
  608. ci->i_wr_ref = 0;
  609. ci->i_wb_ref = 0;
  610. ci->i_fx_ref = 0;
  611. ci->i_wrbuffer_ref = 0;
  612. ci->i_wrbuffer_ref_head = 0;
  613. atomic_set(&ci->i_filelock_ref, 0);
  614. atomic_set(&ci->i_shared_gen, 1);
  615. ci->i_rdcache_gen = 0;
  616. ci->i_rdcache_revoking = 0;
  617. INIT_LIST_HEAD(&ci->i_unsafe_dirops);
  618. INIT_LIST_HEAD(&ci->i_unsafe_iops);
  619. spin_lock_init(&ci->i_unsafe_lock);
  620. ci->i_snap_realm = NULL;
  621. INIT_LIST_HEAD(&ci->i_snap_realm_item);
  622. INIT_LIST_HEAD(&ci->i_snap_flush_item);
  623. INIT_WORK(&ci->i_work, ceph_inode_work);
  624. ci->i_work_mask = 0;
  625. memset(&ci->i_btime, '\0', sizeof(ci->i_btime));
  626. #ifdef CONFIG_FS_ENCRYPTION
  627. ci->fscrypt_auth = NULL;
  628. ci->fscrypt_auth_len = 0;
  629. #endif
  630. return &ci->netfs.inode;
  631. }
  632. void ceph_free_inode(struct inode *inode)
  633. {
  634. struct ceph_inode_info *ci = ceph_inode(inode);
  635. kfree(ci->i_symlink);
  636. #ifdef CONFIG_FS_ENCRYPTION
  637. kfree(ci->fscrypt_auth);
  638. #endif
  639. fscrypt_free_inode(inode);
  640. kmem_cache_free(ceph_inode_cachep, ci);
  641. }
  642. void ceph_evict_inode(struct inode *inode)
  643. {
  644. struct ceph_inode_info *ci = ceph_inode(inode);
  645. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
  646. struct ceph_client *cl = ceph_inode_to_client(inode);
  647. struct ceph_inode_frag *frag;
  648. struct rb_node *n;
  649. doutc(cl, "%p ino %llx.%llx\n", inode, ceph_vinop(inode));
  650. percpu_counter_dec(&mdsc->metric.total_inodes);
  651. netfs_wait_for_outstanding_io(inode);
  652. truncate_inode_pages_final(&inode->i_data);
  653. if (inode->i_state & I_PINNING_NETFS_WB)
  654. ceph_fscache_unuse_cookie(inode, true);
  655. clear_inode(inode);
  656. ceph_fscache_unregister_inode_cookie(ci);
  657. fscrypt_put_encryption_info(inode);
  658. __ceph_remove_caps(ci);
  659. if (__ceph_has_quota(ci, QUOTA_GET_ANY))
  660. ceph_adjust_quota_realms_count(inode, false);
  661. /*
  662. * we may still have a snap_realm reference if there are stray
  663. * caps in i_snap_caps.
  664. */
  665. if (ci->i_snap_realm) {
  666. if (ceph_snap(inode) == CEPH_NOSNAP) {
  667. doutc(cl, " dropping residual ref to snap realm %p\n",
  668. ci->i_snap_realm);
  669. ceph_change_snap_realm(inode, NULL);
  670. } else {
  671. ceph_put_snapid_map(mdsc, ci->i_snapid_map);
  672. ci->i_snap_realm = NULL;
  673. }
  674. }
  675. while ((n = rb_first(&ci->i_fragtree)) != NULL) {
  676. frag = rb_entry(n, struct ceph_inode_frag, node);
  677. rb_erase(n, &ci->i_fragtree);
  678. kfree(frag);
  679. }
  680. ci->i_fragtree_nsplits = 0;
  681. __ceph_destroy_xattrs(ci);
  682. if (ci->i_xattrs.blob)
  683. ceph_buffer_put(ci->i_xattrs.blob);
  684. if (ci->i_xattrs.prealloc_blob)
  685. ceph_buffer_put(ci->i_xattrs.prealloc_blob);
  686. ceph_put_string(rcu_dereference_raw(ci->i_layout.pool_ns));
  687. ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns));
  688. }
  689. static inline blkcnt_t calc_inode_blocks(u64 size)
  690. {
  691. return (size + (1<<9) - 1) >> 9;
  692. }
  693. /*
  694. * Helpers to fill in size, ctime, mtime, and atime. We have to be
  695. * careful because either the client or MDS may have more up to date
  696. * info, depending on which capabilities are held, and whether
  697. * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
  698. * and size are monotonically increasing, except when utimes() or
  699. * truncate() increments the corresponding _seq values.)
  700. */
  701. int ceph_fill_file_size(struct inode *inode, int issued,
  702. u32 truncate_seq, u64 truncate_size, u64 size)
  703. {
  704. struct ceph_client *cl = ceph_inode_to_client(inode);
  705. struct ceph_inode_info *ci = ceph_inode(inode);
  706. int queue_trunc = 0;
  707. loff_t isize = i_size_read(inode);
  708. if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
  709. (truncate_seq == ci->i_truncate_seq && size > isize)) {
  710. doutc(cl, "size %lld -> %llu\n", isize, size);
  711. if (size > 0 && S_ISDIR(inode->i_mode)) {
  712. pr_err_client(cl, "non-zero size for directory\n");
  713. size = 0;
  714. }
  715. i_size_write(inode, size);
  716. inode->i_blocks = calc_inode_blocks(size);
  717. /*
  718. * If we're expanding, then we should be able to just update
  719. * the existing cookie.
  720. */
  721. if (size > isize)
  722. ceph_fscache_update(inode);
  723. ci->i_reported_size = size;
  724. if (truncate_seq != ci->i_truncate_seq) {
  725. doutc(cl, "truncate_seq %u -> %u\n",
  726. ci->i_truncate_seq, truncate_seq);
  727. ci->i_truncate_seq = truncate_seq;
  728. /* the MDS should have revoked these caps */
  729. WARN_ON_ONCE(issued & (CEPH_CAP_FILE_RD |
  730. CEPH_CAP_FILE_LAZYIO));
  731. /*
  732. * If we hold relevant caps, or in the case where we're
  733. * not the only client referencing this file and we
  734. * don't hold those caps, then we need to check whether
  735. * the file is either opened or mmaped
  736. */
  737. if ((issued & (CEPH_CAP_FILE_CACHE|
  738. CEPH_CAP_FILE_BUFFER)) ||
  739. mapping_mapped(inode->i_mapping) ||
  740. __ceph_is_file_opened(ci)) {
  741. ci->i_truncate_pending++;
  742. queue_trunc = 1;
  743. }
  744. }
  745. }
  746. /*
  747. * It's possible that the new sizes of the two consecutive
  748. * size truncations will be in the same fscrypt last block,
  749. * and we need to truncate the corresponding page caches
  750. * anyway.
  751. */
  752. if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0) {
  753. doutc(cl, "truncate_size %lld -> %llu, encrypted %d\n",
  754. ci->i_truncate_size, truncate_size,
  755. !!IS_ENCRYPTED(inode));
  756. ci->i_truncate_size = truncate_size;
  757. if (IS_ENCRYPTED(inode)) {
  758. doutc(cl, "truncate_pagecache_size %lld -> %llu\n",
  759. ci->i_truncate_pagecache_size, size);
  760. ci->i_truncate_pagecache_size = size;
  761. } else {
  762. ci->i_truncate_pagecache_size = truncate_size;
  763. }
  764. }
  765. return queue_trunc;
  766. }
  767. void ceph_fill_file_time(struct inode *inode, int issued,
  768. u64 time_warp_seq, struct timespec64 *ctime,
  769. struct timespec64 *mtime, struct timespec64 *atime)
  770. {
  771. struct ceph_client *cl = ceph_inode_to_client(inode);
  772. struct ceph_inode_info *ci = ceph_inode(inode);
  773. struct timespec64 ictime = inode_get_ctime(inode);
  774. int warn = 0;
  775. if (issued & (CEPH_CAP_FILE_EXCL|
  776. CEPH_CAP_FILE_WR|
  777. CEPH_CAP_FILE_BUFFER|
  778. CEPH_CAP_AUTH_EXCL|
  779. CEPH_CAP_XATTR_EXCL)) {
  780. if (ci->i_version == 0 ||
  781. timespec64_compare(ctime, &ictime) > 0) {
  782. doutc(cl, "ctime %lld.%09ld -> %lld.%09ld inc w/ cap\n",
  783. ictime.tv_sec, ictime.tv_nsec,
  784. ctime->tv_sec, ctime->tv_nsec);
  785. inode_set_ctime_to_ts(inode, *ctime);
  786. }
  787. if (ci->i_version == 0 ||
  788. ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
  789. /* the MDS did a utimes() */
  790. doutc(cl, "mtime %lld.%09ld -> %lld.%09ld tw %d -> %d\n",
  791. inode_get_mtime_sec(inode),
  792. inode_get_mtime_nsec(inode),
  793. mtime->tv_sec, mtime->tv_nsec,
  794. ci->i_time_warp_seq, (int)time_warp_seq);
  795. inode_set_mtime_to_ts(inode, *mtime);
  796. inode_set_atime_to_ts(inode, *atime);
  797. ci->i_time_warp_seq = time_warp_seq;
  798. } else if (time_warp_seq == ci->i_time_warp_seq) {
  799. struct timespec64 ts;
  800. /* nobody did utimes(); take the max */
  801. ts = inode_get_mtime(inode);
  802. if (timespec64_compare(mtime, &ts) > 0) {
  803. doutc(cl, "mtime %lld.%09ld -> %lld.%09ld inc\n",
  804. ts.tv_sec, ts.tv_nsec,
  805. mtime->tv_sec, mtime->tv_nsec);
  806. inode_set_mtime_to_ts(inode, *mtime);
  807. }
  808. ts = inode_get_atime(inode);
  809. if (timespec64_compare(atime, &ts) > 0) {
  810. doutc(cl, "atime %lld.%09ld -> %lld.%09ld inc\n",
  811. ts.tv_sec, ts.tv_nsec,
  812. atime->tv_sec, atime->tv_nsec);
  813. inode_set_atime_to_ts(inode, *atime);
  814. }
  815. } else if (issued & CEPH_CAP_FILE_EXCL) {
  816. /* we did a utimes(); ignore mds values */
  817. } else {
  818. warn = 1;
  819. }
  820. } else {
  821. /* we have no write|excl caps; whatever the MDS says is true */
  822. if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
  823. inode_set_ctime_to_ts(inode, *ctime);
  824. inode_set_mtime_to_ts(inode, *mtime);
  825. inode_set_atime_to_ts(inode, *atime);
  826. ci->i_time_warp_seq = time_warp_seq;
  827. } else {
  828. warn = 1;
  829. }
  830. }
  831. if (warn) /* time_warp_seq shouldn't go backwards */
  832. doutc(cl, "%p mds time_warp_seq %llu < %u\n", inode,
  833. time_warp_seq, ci->i_time_warp_seq);
  834. }
  835. #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
  836. static int decode_encrypted_symlink(struct ceph_mds_client *mdsc,
  837. const char *encsym,
  838. int enclen, u8 **decsym)
  839. {
  840. struct ceph_client *cl = mdsc->fsc->client;
  841. int declen;
  842. u8 *sym;
  843. sym = kmalloc(enclen + 1, GFP_NOFS);
  844. if (!sym)
  845. return -ENOMEM;
  846. declen = ceph_base64_decode(encsym, enclen, sym);
  847. if (declen < 0) {
  848. pr_err_client(cl,
  849. "can't decode symlink (%d). Content: %.*s\n",
  850. declen, enclen, encsym);
  851. kfree(sym);
  852. return -EIO;
  853. }
  854. sym[declen + 1] = '\0';
  855. *decsym = sym;
  856. return declen;
  857. }
  858. #else
  859. static int decode_encrypted_symlink(struct ceph_mds_client *mdsc,
  860. const char *encsym,
  861. int symlen, u8 **decsym)
  862. {
  863. return -EOPNOTSUPP;
  864. }
  865. #endif
  866. /*
  867. * Populate an inode based on info from mds. May be called on new or
  868. * existing inodes.
  869. */
  870. int ceph_fill_inode(struct inode *inode, struct page *locked_page,
  871. struct ceph_mds_reply_info_in *iinfo,
  872. struct ceph_mds_reply_dirfrag *dirinfo,
  873. struct ceph_mds_session *session, int cap_fmode,
  874. struct ceph_cap_reservation *caps_reservation)
  875. {
  876. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
  877. struct ceph_client *cl = mdsc->fsc->client;
  878. struct ceph_mds_reply_inode *info = iinfo->in;
  879. struct ceph_inode_info *ci = ceph_inode(inode);
  880. int issued, new_issued, info_caps;
  881. struct timespec64 mtime, atime, ctime;
  882. struct ceph_buffer *xattr_blob = NULL;
  883. struct ceph_buffer *old_blob = NULL;
  884. struct ceph_string *pool_ns = NULL;
  885. struct ceph_cap *new_cap = NULL;
  886. int err = 0;
  887. bool wake = false;
  888. bool queue_trunc = false;
  889. bool new_version = false;
  890. bool fill_inline = false;
  891. umode_t mode = le32_to_cpu(info->mode);
  892. dev_t rdev = le32_to_cpu(info->rdev);
  893. lockdep_assert_held(&mdsc->snap_rwsem);
  894. doutc(cl, "%p ino %llx.%llx v %llu had %llu\n", inode, ceph_vinop(inode),
  895. le64_to_cpu(info->version), ci->i_version);
  896. /* Once I_NEW is cleared, we can't change type or dev numbers */
  897. if (inode->i_state & I_NEW) {
  898. inode->i_mode = mode;
  899. } else {
  900. if (inode_wrong_type(inode, mode)) {
  901. pr_warn_once_client(cl,
  902. "inode type changed! (ino %llx.%llx is 0%o, mds says 0%o)\n",
  903. ceph_vinop(inode), inode->i_mode, mode);
  904. return -ESTALE;
  905. }
  906. if ((S_ISCHR(mode) || S_ISBLK(mode)) && inode->i_rdev != rdev) {
  907. pr_warn_once_client(cl,
  908. "dev inode rdev changed! (ino %llx.%llx is %u:%u, mds says %u:%u)\n",
  909. ceph_vinop(inode), MAJOR(inode->i_rdev),
  910. MINOR(inode->i_rdev), MAJOR(rdev),
  911. MINOR(rdev));
  912. return -ESTALE;
  913. }
  914. }
  915. info_caps = le32_to_cpu(info->cap.caps);
  916. /* prealloc new cap struct */
  917. if (info_caps && ceph_snap(inode) == CEPH_NOSNAP) {
  918. new_cap = ceph_get_cap(mdsc, caps_reservation);
  919. if (!new_cap)
  920. return -ENOMEM;
  921. }
  922. /*
  923. * prealloc xattr data, if it looks like we'll need it. only
  924. * if len > 4 (meaning there are actually xattrs; the first 4
  925. * bytes are the xattr count).
  926. */
  927. if (iinfo->xattr_len > 4) {
  928. xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
  929. if (!xattr_blob)
  930. pr_err_client(cl, "ENOMEM xattr blob %d bytes\n",
  931. iinfo->xattr_len);
  932. }
  933. if (iinfo->pool_ns_len > 0)
  934. pool_ns = ceph_find_or_create_string(iinfo->pool_ns_data,
  935. iinfo->pool_ns_len);
  936. if (ceph_snap(inode) != CEPH_NOSNAP && !ci->i_snapid_map)
  937. ci->i_snapid_map = ceph_get_snapid_map(mdsc, ceph_snap(inode));
  938. spin_lock(&ci->i_ceph_lock);
  939. /*
  940. * provided version will be odd if inode value is projected,
  941. * even if stable. skip the update if we have newer stable
  942. * info (ours>=theirs, e.g. due to racing mds replies), unless
  943. * we are getting projected (unstable) info (in which case the
  944. * version is odd, and we want ours>theirs).
  945. * us them
  946. * 2 2 skip
  947. * 3 2 skip
  948. * 3 3 update
  949. */
  950. if (ci->i_version == 0 ||
  951. ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
  952. le64_to_cpu(info->version) > (ci->i_version & ~1)))
  953. new_version = true;
  954. /* Update change_attribute */
  955. inode_set_max_iversion_raw(inode, iinfo->change_attr);
  956. __ceph_caps_issued(ci, &issued);
  957. issued |= __ceph_caps_dirty(ci);
  958. new_issued = ~issued & info_caps;
  959. __ceph_update_quota(ci, iinfo->max_bytes, iinfo->max_files);
  960. #ifdef CONFIG_FS_ENCRYPTION
  961. if (iinfo->fscrypt_auth_len &&
  962. ((inode->i_state & I_NEW) || (ci->fscrypt_auth_len == 0))) {
  963. kfree(ci->fscrypt_auth);
  964. ci->fscrypt_auth_len = iinfo->fscrypt_auth_len;
  965. ci->fscrypt_auth = iinfo->fscrypt_auth;
  966. iinfo->fscrypt_auth = NULL;
  967. iinfo->fscrypt_auth_len = 0;
  968. inode_set_flags(inode, S_ENCRYPTED, S_ENCRYPTED);
  969. }
  970. #endif
  971. if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) &&
  972. (issued & CEPH_CAP_AUTH_EXCL) == 0) {
  973. inode->i_mode = mode;
  974. inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid));
  975. inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid));
  976. doutc(cl, "%p %llx.%llx mode 0%o uid.gid %d.%d\n", inode,
  977. ceph_vinop(inode), inode->i_mode,
  978. from_kuid(&init_user_ns, inode->i_uid),
  979. from_kgid(&init_user_ns, inode->i_gid));
  980. ceph_decode_timespec64(&ci->i_btime, &iinfo->btime);
  981. ceph_decode_timespec64(&ci->i_snap_btime, &iinfo->snap_btime);
  982. }
  983. /* directories have fl_stripe_unit set to zero */
  984. if (IS_ENCRYPTED(inode))
  985. inode->i_blkbits = CEPH_FSCRYPT_BLOCK_SHIFT;
  986. else if (le32_to_cpu(info->layout.fl_stripe_unit))
  987. inode->i_blkbits =
  988. fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
  989. else
  990. inode->i_blkbits = CEPH_BLOCK_SHIFT;
  991. if ((new_version || (new_issued & CEPH_CAP_LINK_SHARED)) &&
  992. (issued & CEPH_CAP_LINK_EXCL) == 0)
  993. set_nlink(inode, le32_to_cpu(info->nlink));
  994. if (new_version || (new_issued & CEPH_CAP_ANY_RD)) {
  995. /* be careful with mtime, atime, size */
  996. ceph_decode_timespec64(&atime, &info->atime);
  997. ceph_decode_timespec64(&mtime, &info->mtime);
  998. ceph_decode_timespec64(&ctime, &info->ctime);
  999. ceph_fill_file_time(inode, issued,
  1000. le32_to_cpu(info->time_warp_seq),
  1001. &ctime, &mtime, &atime);
  1002. }
  1003. if (new_version || (info_caps & CEPH_CAP_FILE_SHARED)) {
  1004. ci->i_files = le64_to_cpu(info->files);
  1005. ci->i_subdirs = le64_to_cpu(info->subdirs);
  1006. }
  1007. if (new_version ||
  1008. (new_issued & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR))) {
  1009. u64 size = le64_to_cpu(info->size);
  1010. s64 old_pool = ci->i_layout.pool_id;
  1011. struct ceph_string *old_ns;
  1012. ceph_file_layout_from_legacy(&ci->i_layout, &info->layout);
  1013. old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
  1014. lockdep_is_held(&ci->i_ceph_lock));
  1015. rcu_assign_pointer(ci->i_layout.pool_ns, pool_ns);
  1016. if (ci->i_layout.pool_id != old_pool || pool_ns != old_ns)
  1017. ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
  1018. pool_ns = old_ns;
  1019. if (IS_ENCRYPTED(inode) && size &&
  1020. iinfo->fscrypt_file_len == sizeof(__le64)) {
  1021. u64 fsize = __le64_to_cpu(*(__le64 *)iinfo->fscrypt_file);
  1022. if (size == round_up(fsize, CEPH_FSCRYPT_BLOCK_SIZE)) {
  1023. size = fsize;
  1024. } else {
  1025. pr_warn_client(cl,
  1026. "fscrypt size mismatch: size=%llu fscrypt_file=%llu, discarding fscrypt_file size.\n",
  1027. info->size, size);
  1028. }
  1029. }
  1030. queue_trunc = ceph_fill_file_size(inode, issued,
  1031. le32_to_cpu(info->truncate_seq),
  1032. le64_to_cpu(info->truncate_size),
  1033. size);
  1034. /* only update max_size on auth cap */
  1035. if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
  1036. ci->i_max_size != le64_to_cpu(info->max_size)) {
  1037. doutc(cl, "max_size %lld -> %llu\n",
  1038. ci->i_max_size, le64_to_cpu(info->max_size));
  1039. ci->i_max_size = le64_to_cpu(info->max_size);
  1040. }
  1041. }
  1042. /* layout and rstat are not tracked by capability, update them if
  1043. * the inode info is from auth mds */
  1044. if (new_version || (info->cap.flags & CEPH_CAP_FLAG_AUTH)) {
  1045. if (S_ISDIR(inode->i_mode)) {
  1046. ci->i_dir_layout = iinfo->dir_layout;
  1047. ci->i_rbytes = le64_to_cpu(info->rbytes);
  1048. ci->i_rfiles = le64_to_cpu(info->rfiles);
  1049. ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
  1050. ci->i_dir_pin = iinfo->dir_pin;
  1051. ci->i_rsnaps = iinfo->rsnaps;
  1052. ceph_decode_timespec64(&ci->i_rctime, &info->rctime);
  1053. }
  1054. }
  1055. /* xattrs */
  1056. /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
  1057. if ((ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) &&
  1058. le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
  1059. if (ci->i_xattrs.blob)
  1060. old_blob = ci->i_xattrs.blob;
  1061. ci->i_xattrs.blob = xattr_blob;
  1062. if (xattr_blob)
  1063. memcpy(ci->i_xattrs.blob->vec.iov_base,
  1064. iinfo->xattr_data, iinfo->xattr_len);
  1065. ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
  1066. ceph_forget_all_cached_acls(inode);
  1067. ceph_security_invalidate_secctx(inode);
  1068. xattr_blob = NULL;
  1069. }
  1070. /* finally update i_version */
  1071. if (le64_to_cpu(info->version) > ci->i_version)
  1072. ci->i_version = le64_to_cpu(info->version);
  1073. inode->i_mapping->a_ops = &ceph_aops;
  1074. switch (inode->i_mode & S_IFMT) {
  1075. case S_IFIFO:
  1076. case S_IFBLK:
  1077. case S_IFCHR:
  1078. case S_IFSOCK:
  1079. inode->i_blkbits = PAGE_SHIFT;
  1080. init_special_inode(inode, inode->i_mode, rdev);
  1081. inode->i_op = &ceph_file_iops;
  1082. break;
  1083. case S_IFREG:
  1084. inode->i_op = &ceph_file_iops;
  1085. inode->i_fop = &ceph_file_fops;
  1086. break;
  1087. case S_IFLNK:
  1088. if (!ci->i_symlink) {
  1089. u32 symlen = iinfo->symlink_len;
  1090. char *sym;
  1091. spin_unlock(&ci->i_ceph_lock);
  1092. if (IS_ENCRYPTED(inode)) {
  1093. if (symlen != i_size_read(inode))
  1094. pr_err_client(cl,
  1095. "%p %llx.%llx BAD symlink size %lld\n",
  1096. inode, ceph_vinop(inode),
  1097. i_size_read(inode));
  1098. err = decode_encrypted_symlink(mdsc, iinfo->symlink,
  1099. symlen, (u8 **)&sym);
  1100. if (err < 0) {
  1101. pr_err_client(cl,
  1102. "decoding encrypted symlink failed: %d\n",
  1103. err);
  1104. goto out;
  1105. }
  1106. symlen = err;
  1107. i_size_write(inode, symlen);
  1108. inode->i_blocks = calc_inode_blocks(symlen);
  1109. } else {
  1110. if (symlen != i_size_read(inode)) {
  1111. pr_err_client(cl,
  1112. "%p %llx.%llx BAD symlink size %lld\n",
  1113. inode, ceph_vinop(inode),
  1114. i_size_read(inode));
  1115. i_size_write(inode, symlen);
  1116. inode->i_blocks = calc_inode_blocks(symlen);
  1117. }
  1118. err = -ENOMEM;
  1119. sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS);
  1120. if (!sym)
  1121. goto out;
  1122. }
  1123. spin_lock(&ci->i_ceph_lock);
  1124. if (!ci->i_symlink)
  1125. ci->i_symlink = sym;
  1126. else
  1127. kfree(sym); /* lost a race */
  1128. }
  1129. if (IS_ENCRYPTED(inode)) {
  1130. /*
  1131. * Encrypted symlinks need to be decrypted before we can
  1132. * cache their targets in i_link. Don't touch it here.
  1133. */
  1134. inode->i_op = &ceph_encrypted_symlink_iops;
  1135. } else {
  1136. inode->i_link = ci->i_symlink;
  1137. inode->i_op = &ceph_symlink_iops;
  1138. }
  1139. break;
  1140. case S_IFDIR:
  1141. inode->i_op = &ceph_dir_iops;
  1142. inode->i_fop = &ceph_dir_fops;
  1143. break;
  1144. default:
  1145. pr_err_client(cl, "%p %llx.%llx BAD mode 0%o\n", inode,
  1146. ceph_vinop(inode), inode->i_mode);
  1147. }
  1148. /* were we issued a capability? */
  1149. if (info_caps) {
  1150. if (ceph_snap(inode) == CEPH_NOSNAP) {
  1151. ceph_add_cap(inode, session,
  1152. le64_to_cpu(info->cap.cap_id),
  1153. info_caps,
  1154. le32_to_cpu(info->cap.wanted),
  1155. le32_to_cpu(info->cap.seq),
  1156. le32_to_cpu(info->cap.mseq),
  1157. le64_to_cpu(info->cap.realm),
  1158. info->cap.flags, &new_cap);
  1159. /* set dir completion flag? */
  1160. if (S_ISDIR(inode->i_mode) &&
  1161. ci->i_files == 0 && ci->i_subdirs == 0 &&
  1162. (info_caps & CEPH_CAP_FILE_SHARED) &&
  1163. (issued & CEPH_CAP_FILE_EXCL) == 0 &&
  1164. !__ceph_dir_is_complete(ci)) {
  1165. doutc(cl, " marking %p complete (empty)\n",
  1166. inode);
  1167. i_size_write(inode, 0);
  1168. __ceph_dir_set_complete(ci,
  1169. atomic64_read(&ci->i_release_count),
  1170. atomic64_read(&ci->i_ordered_count));
  1171. }
  1172. wake = true;
  1173. } else {
  1174. doutc(cl, " %p got snap_caps %s\n", inode,
  1175. ceph_cap_string(info_caps));
  1176. ci->i_snap_caps |= info_caps;
  1177. }
  1178. }
  1179. if (iinfo->inline_version > 0 &&
  1180. iinfo->inline_version >= ci->i_inline_version) {
  1181. int cache_caps = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
  1182. ci->i_inline_version = iinfo->inline_version;
  1183. if (ceph_has_inline_data(ci) &&
  1184. (locked_page || (info_caps & cache_caps)))
  1185. fill_inline = true;
  1186. }
  1187. if (cap_fmode >= 0) {
  1188. if (!info_caps)
  1189. pr_warn_client(cl, "mds issued no caps on %llx.%llx\n",
  1190. ceph_vinop(inode));
  1191. __ceph_touch_fmode(ci, mdsc, cap_fmode);
  1192. }
  1193. spin_unlock(&ci->i_ceph_lock);
  1194. ceph_fscache_register_inode_cookie(inode);
  1195. if (fill_inline)
  1196. ceph_fill_inline_data(inode, locked_page,
  1197. iinfo->inline_data, iinfo->inline_len);
  1198. if (wake)
  1199. wake_up_all(&ci->i_cap_wq);
  1200. /* queue truncate if we saw i_size decrease */
  1201. if (queue_trunc)
  1202. ceph_queue_vmtruncate(inode);
  1203. /* populate frag tree */
  1204. if (S_ISDIR(inode->i_mode))
  1205. ceph_fill_fragtree(inode, &info->fragtree, dirinfo);
  1206. /* update delegation info? */
  1207. if (dirinfo)
  1208. ceph_fill_dirfrag(inode, dirinfo);
  1209. err = 0;
  1210. out:
  1211. if (new_cap)
  1212. ceph_put_cap(mdsc, new_cap);
  1213. ceph_buffer_put(old_blob);
  1214. ceph_buffer_put(xattr_blob);
  1215. ceph_put_string(pool_ns);
  1216. return err;
  1217. }
  1218. /*
  1219. * caller should hold session s_mutex and dentry->d_lock.
  1220. */
  1221. static void __update_dentry_lease(struct inode *dir, struct dentry *dentry,
  1222. struct ceph_mds_reply_lease *lease,
  1223. struct ceph_mds_session *session,
  1224. unsigned long from_time,
  1225. struct ceph_mds_session **old_lease_session)
  1226. {
  1227. struct ceph_client *cl = ceph_inode_to_client(dir);
  1228. struct ceph_dentry_info *di = ceph_dentry(dentry);
  1229. unsigned mask = le16_to_cpu(lease->mask);
  1230. long unsigned duration = le32_to_cpu(lease->duration_ms);
  1231. long unsigned ttl = from_time + (duration * HZ) / 1000;
  1232. long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
  1233. doutc(cl, "%p duration %lu ms ttl %lu\n", dentry, duration, ttl);
  1234. /* only track leases on regular dentries */
  1235. if (ceph_snap(dir) != CEPH_NOSNAP)
  1236. return;
  1237. if (mask & CEPH_LEASE_PRIMARY_LINK)
  1238. di->flags |= CEPH_DENTRY_PRIMARY_LINK;
  1239. else
  1240. di->flags &= ~CEPH_DENTRY_PRIMARY_LINK;
  1241. di->lease_shared_gen = atomic_read(&ceph_inode(dir)->i_shared_gen);
  1242. if (!(mask & CEPH_LEASE_VALID)) {
  1243. __ceph_dentry_dir_lease_touch(di);
  1244. return;
  1245. }
  1246. if (di->lease_gen == atomic_read(&session->s_cap_gen) &&
  1247. time_before(ttl, di->time))
  1248. return; /* we already have a newer lease. */
  1249. if (di->lease_session && di->lease_session != session) {
  1250. *old_lease_session = di->lease_session;
  1251. di->lease_session = NULL;
  1252. }
  1253. if (!di->lease_session)
  1254. di->lease_session = ceph_get_mds_session(session);
  1255. di->lease_gen = atomic_read(&session->s_cap_gen);
  1256. di->lease_seq = le32_to_cpu(lease->seq);
  1257. di->lease_renew_after = half_ttl;
  1258. di->lease_renew_from = 0;
  1259. di->time = ttl;
  1260. __ceph_dentry_lease_touch(di);
  1261. }
  1262. static inline void update_dentry_lease(struct inode *dir, struct dentry *dentry,
  1263. struct ceph_mds_reply_lease *lease,
  1264. struct ceph_mds_session *session,
  1265. unsigned long from_time)
  1266. {
  1267. struct ceph_mds_session *old_lease_session = NULL;
  1268. spin_lock(&dentry->d_lock);
  1269. __update_dentry_lease(dir, dentry, lease, session, from_time,
  1270. &old_lease_session);
  1271. spin_unlock(&dentry->d_lock);
  1272. ceph_put_mds_session(old_lease_session);
  1273. }
  1274. /*
  1275. * update dentry lease without having parent inode locked
  1276. */
  1277. static void update_dentry_lease_careful(struct dentry *dentry,
  1278. struct ceph_mds_reply_lease *lease,
  1279. struct ceph_mds_session *session,
  1280. unsigned long from_time,
  1281. char *dname, u32 dname_len,
  1282. struct ceph_vino *pdvino,
  1283. struct ceph_vino *ptvino)
  1284. {
  1285. struct inode *dir;
  1286. struct ceph_mds_session *old_lease_session = NULL;
  1287. spin_lock(&dentry->d_lock);
  1288. /* make sure dentry's name matches target */
  1289. if (dentry->d_name.len != dname_len ||
  1290. memcmp(dentry->d_name.name, dname, dname_len))
  1291. goto out_unlock;
  1292. dir = d_inode(dentry->d_parent);
  1293. /* make sure parent matches dvino */
  1294. if (!ceph_ino_compare(dir, pdvino))
  1295. goto out_unlock;
  1296. /* make sure dentry's inode matches target. NULL ptvino means that
  1297. * we expect a negative dentry */
  1298. if (ptvino) {
  1299. if (d_really_is_negative(dentry))
  1300. goto out_unlock;
  1301. if (!ceph_ino_compare(d_inode(dentry), ptvino))
  1302. goto out_unlock;
  1303. } else {
  1304. if (d_really_is_positive(dentry))
  1305. goto out_unlock;
  1306. }
  1307. __update_dentry_lease(dir, dentry, lease, session,
  1308. from_time, &old_lease_session);
  1309. out_unlock:
  1310. spin_unlock(&dentry->d_lock);
  1311. ceph_put_mds_session(old_lease_session);
  1312. }
  1313. /*
  1314. * splice a dentry to an inode.
  1315. * caller must hold directory i_rwsem for this to be safe.
  1316. */
  1317. static int splice_dentry(struct dentry **pdn, struct inode *in)
  1318. {
  1319. struct ceph_client *cl = ceph_inode_to_client(in);
  1320. struct dentry *dn = *pdn;
  1321. struct dentry *realdn;
  1322. BUG_ON(d_inode(dn));
  1323. if (S_ISDIR(in->i_mode)) {
  1324. /* If inode is directory, d_splice_alias() below will remove
  1325. * 'realdn' from its origin parent. We need to ensure that
  1326. * origin parent's readdir cache will not reference 'realdn'
  1327. */
  1328. realdn = d_find_any_alias(in);
  1329. if (realdn) {
  1330. struct ceph_dentry_info *di = ceph_dentry(realdn);
  1331. spin_lock(&realdn->d_lock);
  1332. realdn->d_op->d_prune(realdn);
  1333. di->time = jiffies;
  1334. di->lease_shared_gen = 0;
  1335. di->offset = 0;
  1336. spin_unlock(&realdn->d_lock);
  1337. dput(realdn);
  1338. }
  1339. }
  1340. /* dn must be unhashed */
  1341. if (!d_unhashed(dn))
  1342. d_drop(dn);
  1343. realdn = d_splice_alias(in, dn);
  1344. if (IS_ERR(realdn)) {
  1345. pr_err_client(cl, "error %ld %p inode %p ino %llx.%llx\n",
  1346. PTR_ERR(realdn), dn, in, ceph_vinop(in));
  1347. return PTR_ERR(realdn);
  1348. }
  1349. if (realdn) {
  1350. doutc(cl, "dn %p (%d) spliced with %p (%d) inode %p ino %llx.%llx\n",
  1351. dn, d_count(dn), realdn, d_count(realdn),
  1352. d_inode(realdn), ceph_vinop(d_inode(realdn)));
  1353. dput(dn);
  1354. *pdn = realdn;
  1355. } else {
  1356. BUG_ON(!ceph_dentry(dn));
  1357. doutc(cl, "dn %p attached to %p ino %llx.%llx\n", dn,
  1358. d_inode(dn), ceph_vinop(d_inode(dn)));
  1359. }
  1360. return 0;
  1361. }
  1362. /*
  1363. * Incorporate results into the local cache. This is either just
  1364. * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
  1365. * after a lookup).
  1366. *
  1367. * A reply may contain
  1368. * a directory inode along with a dentry.
  1369. * and/or a target inode
  1370. *
  1371. * Called with snap_rwsem (read).
  1372. */
  1373. int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req)
  1374. {
  1375. struct ceph_mds_session *session = req->r_session;
  1376. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  1377. struct inode *in = NULL;
  1378. struct ceph_vino tvino, dvino;
  1379. struct ceph_fs_client *fsc = ceph_sb_to_fs_client(sb);
  1380. struct ceph_client *cl = fsc->client;
  1381. struct inode *parent_dir = NULL;
  1382. int err = 0;
  1383. doutc(cl, "%p is_dentry %d is_target %d\n", req,
  1384. rinfo->head->is_dentry, rinfo->head->is_target);
  1385. if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
  1386. doutc(cl, "reply is empty!\n");
  1387. if (rinfo->head->result == 0 && req->r_parent)
  1388. ceph_invalidate_dir_request(req);
  1389. return 0;
  1390. }
  1391. if (rinfo->head->is_dentry) {
  1392. /*
  1393. * r_parent may be stale, in cases when R_PARENT_LOCKED is not set,
  1394. * so we need to get the correct inode
  1395. */
  1396. parent_dir = ceph_get_reply_dir(sb, req->r_parent, rinfo);
  1397. if (unlikely(IS_ERR(parent_dir))) {
  1398. err = PTR_ERR(parent_dir);
  1399. goto done;
  1400. }
  1401. if (parent_dir) {
  1402. err = ceph_fill_inode(parent_dir, NULL, &rinfo->diri,
  1403. rinfo->dirfrag, session, -1,
  1404. &req->r_caps_reservation);
  1405. if (err < 0)
  1406. goto done;
  1407. } else {
  1408. WARN_ON_ONCE(1);
  1409. }
  1410. if (parent_dir && req->r_op == CEPH_MDS_OP_LOOKUPNAME &&
  1411. test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
  1412. !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
  1413. bool is_nokey = false;
  1414. struct qstr dname;
  1415. struct dentry *dn, *parent;
  1416. struct fscrypt_str oname = FSTR_INIT(NULL, 0);
  1417. struct ceph_fname fname = { .dir = parent_dir,
  1418. .name = rinfo->dname,
  1419. .ctext = rinfo->altname,
  1420. .name_len = rinfo->dname_len,
  1421. .ctext_len = rinfo->altname_len };
  1422. BUG_ON(!rinfo->head->is_target);
  1423. BUG_ON(req->r_dentry);
  1424. parent = d_find_any_alias(parent_dir);
  1425. BUG_ON(!parent);
  1426. err = ceph_fname_alloc_buffer(parent_dir, &oname);
  1427. if (err < 0) {
  1428. dput(parent);
  1429. goto done;
  1430. }
  1431. err = ceph_fname_to_usr(&fname, NULL, &oname, &is_nokey);
  1432. if (err < 0) {
  1433. dput(parent);
  1434. ceph_fname_free_buffer(parent_dir, &oname);
  1435. goto done;
  1436. }
  1437. dname.name = oname.name;
  1438. dname.len = oname.len;
  1439. dname.hash = full_name_hash(parent, dname.name, dname.len);
  1440. tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
  1441. tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
  1442. retry_lookup:
  1443. dn = d_lookup(parent, &dname);
  1444. doutc(cl, "d_lookup on parent=%p name=%.*s got %p\n",
  1445. parent, dname.len, dname.name, dn);
  1446. if (!dn) {
  1447. dn = d_alloc(parent, &dname);
  1448. doutc(cl, "d_alloc %p '%.*s' = %p\n", parent,
  1449. dname.len, dname.name, dn);
  1450. if (!dn) {
  1451. dput(parent);
  1452. ceph_fname_free_buffer(parent_dir, &oname);
  1453. err = -ENOMEM;
  1454. goto done;
  1455. }
  1456. if (is_nokey) {
  1457. spin_lock(&dn->d_lock);
  1458. dn->d_flags |= DCACHE_NOKEY_NAME;
  1459. spin_unlock(&dn->d_lock);
  1460. }
  1461. err = 0;
  1462. } else if (d_really_is_positive(dn) &&
  1463. (ceph_ino(d_inode(dn)) != tvino.ino ||
  1464. ceph_snap(d_inode(dn)) != tvino.snap)) {
  1465. doutc(cl, " dn %p points to wrong inode %p\n",
  1466. dn, d_inode(dn));
  1467. ceph_dir_clear_ordered(parent_dir);
  1468. d_delete(dn);
  1469. dput(dn);
  1470. goto retry_lookup;
  1471. }
  1472. ceph_fname_free_buffer(parent_dir, &oname);
  1473. req->r_dentry = dn;
  1474. dput(parent);
  1475. }
  1476. }
  1477. if (rinfo->head->is_target) {
  1478. /* Should be filled in by handle_reply */
  1479. BUG_ON(!req->r_target_inode);
  1480. in = req->r_target_inode;
  1481. err = ceph_fill_inode(in, req->r_locked_page, &rinfo->targeti,
  1482. NULL, session,
  1483. (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
  1484. !test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags) &&
  1485. rinfo->head->result == 0) ? req->r_fmode : -1,
  1486. &req->r_caps_reservation);
  1487. if (err < 0) {
  1488. pr_err_client(cl, "badness %p %llx.%llx\n", in,
  1489. ceph_vinop(in));
  1490. req->r_target_inode = NULL;
  1491. if (in->i_state & I_NEW)
  1492. discard_new_inode(in);
  1493. else
  1494. iput(in);
  1495. goto done;
  1496. }
  1497. if (in->i_state & I_NEW)
  1498. unlock_new_inode(in);
  1499. }
  1500. /*
  1501. * ignore null lease/binding on snapdir ENOENT, or else we
  1502. * will have trouble splicing in the virtual snapdir later
  1503. */
  1504. if (rinfo->head->is_dentry &&
  1505. !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
  1506. test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
  1507. (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
  1508. fsc->mount_options->snapdir_name,
  1509. req->r_dentry->d_name.len))) {
  1510. /*
  1511. * lookup link rename : null -> possibly existing inode
  1512. * mknod symlink mkdir : null -> new inode
  1513. * unlink : linked -> null
  1514. */
  1515. struct inode *dir = req->r_parent;
  1516. struct dentry *dn = req->r_dentry;
  1517. bool have_dir_cap, have_lease;
  1518. BUG_ON(!dn);
  1519. BUG_ON(!dir);
  1520. BUG_ON(d_inode(dn->d_parent) != dir);
  1521. dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
  1522. dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
  1523. BUG_ON(ceph_ino(dir) != dvino.ino);
  1524. BUG_ON(ceph_snap(dir) != dvino.snap);
  1525. /* do we have a lease on the whole dir? */
  1526. have_dir_cap =
  1527. (le32_to_cpu(rinfo->diri.in->cap.caps) &
  1528. CEPH_CAP_FILE_SHARED);
  1529. /* do we have a dn lease? */
  1530. have_lease = have_dir_cap ||
  1531. le32_to_cpu(rinfo->dlease->duration_ms);
  1532. if (!have_lease)
  1533. doutc(cl, "no dentry lease or dir cap\n");
  1534. /* rename? */
  1535. if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
  1536. struct inode *olddir = req->r_old_dentry_dir;
  1537. BUG_ON(!olddir);
  1538. doutc(cl, " src %p '%pd' dst %p '%pd'\n",
  1539. req->r_old_dentry, req->r_old_dentry, dn, dn);
  1540. doutc(cl, "doing d_move %p -> %p\n", req->r_old_dentry, dn);
  1541. /* d_move screws up sibling dentries' offsets */
  1542. ceph_dir_clear_ordered(dir);
  1543. ceph_dir_clear_ordered(olddir);
  1544. d_move(req->r_old_dentry, dn);
  1545. doutc(cl, " src %p '%pd' dst %p '%pd'\n",
  1546. req->r_old_dentry, req->r_old_dentry, dn, dn);
  1547. /* ensure target dentry is invalidated, despite
  1548. rehashing bug in vfs_rename_dir */
  1549. ceph_invalidate_dentry_lease(dn);
  1550. doutc(cl, "dn %p gets new offset %lld\n",
  1551. req->r_old_dentry,
  1552. ceph_dentry(req->r_old_dentry)->offset);
  1553. /* swap r_dentry and r_old_dentry in case that
  1554. * splice_dentry() gets called later. This is safe
  1555. * because no other place will use them */
  1556. req->r_dentry = req->r_old_dentry;
  1557. req->r_old_dentry = dn;
  1558. dn = req->r_dentry;
  1559. }
  1560. /* null dentry? */
  1561. if (!rinfo->head->is_target) {
  1562. doutc(cl, "null dentry\n");
  1563. if (d_really_is_positive(dn)) {
  1564. doutc(cl, "d_delete %p\n", dn);
  1565. ceph_dir_clear_ordered(dir);
  1566. d_delete(dn);
  1567. } else if (have_lease) {
  1568. if (d_unhashed(dn))
  1569. d_add(dn, NULL);
  1570. }
  1571. if (!d_unhashed(dn) && have_lease)
  1572. update_dentry_lease(dir, dn,
  1573. rinfo->dlease, session,
  1574. req->r_request_started);
  1575. goto done;
  1576. }
  1577. /* attach proper inode */
  1578. if (d_really_is_negative(dn)) {
  1579. ceph_dir_clear_ordered(dir);
  1580. ihold(in);
  1581. err = splice_dentry(&req->r_dentry, in);
  1582. if (err < 0)
  1583. goto done;
  1584. dn = req->r_dentry; /* may have spliced */
  1585. } else if (d_really_is_positive(dn) && d_inode(dn) != in) {
  1586. doutc(cl, " %p links to %p %llx.%llx, not %llx.%llx\n",
  1587. dn, d_inode(dn), ceph_vinop(d_inode(dn)),
  1588. ceph_vinop(in));
  1589. d_invalidate(dn);
  1590. have_lease = false;
  1591. }
  1592. if (have_lease) {
  1593. update_dentry_lease(dir, dn,
  1594. rinfo->dlease, session,
  1595. req->r_request_started);
  1596. }
  1597. doutc(cl, " final dn %p\n", dn);
  1598. } else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
  1599. req->r_op == CEPH_MDS_OP_MKSNAP) &&
  1600. test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
  1601. !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
  1602. struct inode *dir = req->r_parent;
  1603. /* fill out a snapdir LOOKUPSNAP dentry */
  1604. BUG_ON(!dir);
  1605. BUG_ON(ceph_snap(dir) != CEPH_SNAPDIR);
  1606. BUG_ON(!req->r_dentry);
  1607. doutc(cl, " linking snapped dir %p to dn %p\n", in,
  1608. req->r_dentry);
  1609. ceph_dir_clear_ordered(dir);
  1610. ihold(in);
  1611. err = splice_dentry(&req->r_dentry, in);
  1612. if (err < 0)
  1613. goto done;
  1614. } else if (rinfo->head->is_dentry && req->r_dentry) {
  1615. /* parent inode is not locked, be careful */
  1616. struct ceph_vino *ptvino = NULL;
  1617. dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
  1618. dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
  1619. if (rinfo->head->is_target) {
  1620. tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
  1621. tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
  1622. ptvino = &tvino;
  1623. }
  1624. update_dentry_lease_careful(req->r_dentry, rinfo->dlease,
  1625. session, req->r_request_started,
  1626. rinfo->dname, rinfo->dname_len,
  1627. &dvino, ptvino);
  1628. }
  1629. done:
  1630. /* Drop extra ref from ceph_get_reply_dir() if it returned a new inode */
  1631. if (unlikely(!IS_ERR_OR_NULL(parent_dir) && parent_dir != req->r_parent))
  1632. iput(parent_dir);
  1633. doutc(cl, "done err=%d\n", err);
  1634. return err;
  1635. }
  1636. /*
  1637. * Prepopulate our cache with readdir results, leases, etc.
  1638. */
  1639. static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req,
  1640. struct ceph_mds_session *session)
  1641. {
  1642. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  1643. struct ceph_client *cl = session->s_mdsc->fsc->client;
  1644. int i, err = 0;
  1645. for (i = 0; i < rinfo->dir_nr; i++) {
  1646. struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
  1647. struct ceph_vino vino;
  1648. struct inode *in;
  1649. int rc;
  1650. vino.ino = le64_to_cpu(rde->inode.in->ino);
  1651. vino.snap = le64_to_cpu(rde->inode.in->snapid);
  1652. in = ceph_get_inode(req->r_dentry->d_sb, vino, NULL);
  1653. if (IS_ERR(in)) {
  1654. err = PTR_ERR(in);
  1655. doutc(cl, "badness got %d\n", err);
  1656. continue;
  1657. }
  1658. rc = ceph_fill_inode(in, NULL, &rde->inode, NULL, session,
  1659. -1, &req->r_caps_reservation);
  1660. if (rc < 0) {
  1661. pr_err_client(cl, "inode badness on %p got %d\n", in,
  1662. rc);
  1663. err = rc;
  1664. if (in->i_state & I_NEW) {
  1665. ihold(in);
  1666. discard_new_inode(in);
  1667. }
  1668. } else if (in->i_state & I_NEW) {
  1669. unlock_new_inode(in);
  1670. }
  1671. iput(in);
  1672. }
  1673. return err;
  1674. }
  1675. void ceph_readdir_cache_release(struct ceph_readdir_cache_control *ctl)
  1676. {
  1677. if (ctl->page) {
  1678. kunmap(ctl->page);
  1679. put_page(ctl->page);
  1680. ctl->page = NULL;
  1681. }
  1682. }
  1683. static int fill_readdir_cache(struct inode *dir, struct dentry *dn,
  1684. struct ceph_readdir_cache_control *ctl,
  1685. struct ceph_mds_request *req)
  1686. {
  1687. struct ceph_client *cl = ceph_inode_to_client(dir);
  1688. struct ceph_inode_info *ci = ceph_inode(dir);
  1689. unsigned nsize = PAGE_SIZE / sizeof(struct dentry*);
  1690. unsigned idx = ctl->index % nsize;
  1691. pgoff_t pgoff = ctl->index / nsize;
  1692. if (!ctl->page || pgoff != ctl->page->index) {
  1693. ceph_readdir_cache_release(ctl);
  1694. if (idx == 0)
  1695. ctl->page = grab_cache_page(&dir->i_data, pgoff);
  1696. else
  1697. ctl->page = find_lock_page(&dir->i_data, pgoff);
  1698. if (!ctl->page) {
  1699. ctl->index = -1;
  1700. return idx == 0 ? -ENOMEM : 0;
  1701. }
  1702. /* reading/filling the cache are serialized by
  1703. * i_rwsem, no need to use page lock */
  1704. unlock_page(ctl->page);
  1705. ctl->dentries = kmap(ctl->page);
  1706. if (idx == 0)
  1707. memset(ctl->dentries, 0, PAGE_SIZE);
  1708. }
  1709. if (req->r_dir_release_cnt == atomic64_read(&ci->i_release_count) &&
  1710. req->r_dir_ordered_cnt == atomic64_read(&ci->i_ordered_count)) {
  1711. doutc(cl, "dn %p idx %d\n", dn, ctl->index);
  1712. ctl->dentries[idx] = dn;
  1713. ctl->index++;
  1714. } else {
  1715. doutc(cl, "disable readdir cache\n");
  1716. ctl->index = -1;
  1717. }
  1718. return 0;
  1719. }
  1720. int ceph_readdir_prepopulate(struct ceph_mds_request *req,
  1721. struct ceph_mds_session *session)
  1722. {
  1723. struct dentry *parent = req->r_dentry;
  1724. struct inode *inode = d_inode(parent);
  1725. struct ceph_inode_info *ci = ceph_inode(inode);
  1726. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  1727. struct ceph_client *cl = session->s_mdsc->fsc->client;
  1728. struct qstr dname;
  1729. struct dentry *dn;
  1730. struct inode *in;
  1731. int err = 0, skipped = 0, ret, i;
  1732. u32 frag = le32_to_cpu(req->r_args.readdir.frag);
  1733. u32 last_hash = 0;
  1734. u32 fpos_offset;
  1735. struct ceph_readdir_cache_control cache_ctl = {};
  1736. if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
  1737. return readdir_prepopulate_inodes_only(req, session);
  1738. if (rinfo->hash_order) {
  1739. if (req->r_path2) {
  1740. last_hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash,
  1741. req->r_path2,
  1742. strlen(req->r_path2));
  1743. last_hash = ceph_frag_value(last_hash);
  1744. } else if (rinfo->offset_hash) {
  1745. /* mds understands offset_hash */
  1746. WARN_ON_ONCE(req->r_readdir_offset != 2);
  1747. last_hash = le32_to_cpu(req->r_args.readdir.offset_hash);
  1748. }
  1749. }
  1750. if (rinfo->dir_dir &&
  1751. le32_to_cpu(rinfo->dir_dir->frag) != frag) {
  1752. doutc(cl, "got new frag %x -> %x\n", frag,
  1753. le32_to_cpu(rinfo->dir_dir->frag));
  1754. frag = le32_to_cpu(rinfo->dir_dir->frag);
  1755. if (!rinfo->hash_order)
  1756. req->r_readdir_offset = 2;
  1757. }
  1758. if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
  1759. doutc(cl, "%d items under SNAPDIR dn %p\n",
  1760. rinfo->dir_nr, parent);
  1761. } else {
  1762. doutc(cl, "%d items under dn %p\n", rinfo->dir_nr, parent);
  1763. if (rinfo->dir_dir)
  1764. ceph_fill_dirfrag(d_inode(parent), rinfo->dir_dir);
  1765. if (ceph_frag_is_leftmost(frag) &&
  1766. req->r_readdir_offset == 2 &&
  1767. !(rinfo->hash_order && last_hash)) {
  1768. /* note dir version at start of readdir so we can
  1769. * tell if any dentries get dropped */
  1770. req->r_dir_release_cnt =
  1771. atomic64_read(&ci->i_release_count);
  1772. req->r_dir_ordered_cnt =
  1773. atomic64_read(&ci->i_ordered_count);
  1774. req->r_readdir_cache_idx = 0;
  1775. }
  1776. }
  1777. cache_ctl.index = req->r_readdir_cache_idx;
  1778. fpos_offset = req->r_readdir_offset;
  1779. /* FIXME: release caps/leases if error occurs */
  1780. for (i = 0; i < rinfo->dir_nr; i++) {
  1781. struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
  1782. struct ceph_vino tvino;
  1783. dname.name = rde->name;
  1784. dname.len = rde->name_len;
  1785. dname.hash = full_name_hash(parent, dname.name, dname.len);
  1786. tvino.ino = le64_to_cpu(rde->inode.in->ino);
  1787. tvino.snap = le64_to_cpu(rde->inode.in->snapid);
  1788. if (rinfo->hash_order) {
  1789. u32 hash = ceph_frag_value(rde->raw_hash);
  1790. if (hash != last_hash)
  1791. fpos_offset = 2;
  1792. last_hash = hash;
  1793. rde->offset = ceph_make_fpos(hash, fpos_offset++, true);
  1794. } else {
  1795. rde->offset = ceph_make_fpos(frag, fpos_offset++, false);
  1796. }
  1797. retry_lookup:
  1798. dn = d_lookup(parent, &dname);
  1799. doutc(cl, "d_lookup on parent=%p name=%.*s got %p\n",
  1800. parent, dname.len, dname.name, dn);
  1801. if (!dn) {
  1802. dn = d_alloc(parent, &dname);
  1803. doutc(cl, "d_alloc %p '%.*s' = %p\n", parent,
  1804. dname.len, dname.name, dn);
  1805. if (!dn) {
  1806. doutc(cl, "d_alloc badness\n");
  1807. err = -ENOMEM;
  1808. goto out;
  1809. }
  1810. if (rde->is_nokey) {
  1811. spin_lock(&dn->d_lock);
  1812. dn->d_flags |= DCACHE_NOKEY_NAME;
  1813. spin_unlock(&dn->d_lock);
  1814. }
  1815. } else if (d_really_is_positive(dn) &&
  1816. (ceph_ino(d_inode(dn)) != tvino.ino ||
  1817. ceph_snap(d_inode(dn)) != tvino.snap)) {
  1818. struct ceph_dentry_info *di = ceph_dentry(dn);
  1819. doutc(cl, " dn %p points to wrong inode %p\n",
  1820. dn, d_inode(dn));
  1821. spin_lock(&dn->d_lock);
  1822. if (di->offset > 0 &&
  1823. di->lease_shared_gen ==
  1824. atomic_read(&ci->i_shared_gen)) {
  1825. __ceph_dir_clear_ordered(ci);
  1826. di->offset = 0;
  1827. }
  1828. spin_unlock(&dn->d_lock);
  1829. d_delete(dn);
  1830. dput(dn);
  1831. goto retry_lookup;
  1832. }
  1833. /* inode */
  1834. if (d_really_is_positive(dn)) {
  1835. in = d_inode(dn);
  1836. } else {
  1837. in = ceph_get_inode(parent->d_sb, tvino, NULL);
  1838. if (IS_ERR(in)) {
  1839. doutc(cl, "new_inode badness\n");
  1840. d_drop(dn);
  1841. dput(dn);
  1842. err = PTR_ERR(in);
  1843. goto out;
  1844. }
  1845. }
  1846. ret = ceph_fill_inode(in, NULL, &rde->inode, NULL, session,
  1847. -1, &req->r_caps_reservation);
  1848. if (ret < 0) {
  1849. pr_err_client(cl, "badness on %p %llx.%llx\n", in,
  1850. ceph_vinop(in));
  1851. if (d_really_is_negative(dn)) {
  1852. if (in->i_state & I_NEW) {
  1853. ihold(in);
  1854. discard_new_inode(in);
  1855. }
  1856. iput(in);
  1857. }
  1858. d_drop(dn);
  1859. err = ret;
  1860. goto next_item;
  1861. }
  1862. if (in->i_state & I_NEW)
  1863. unlock_new_inode(in);
  1864. if (d_really_is_negative(dn)) {
  1865. if (ceph_security_xattr_deadlock(in)) {
  1866. doutc(cl, " skip splicing dn %p to inode %p"
  1867. " (security xattr deadlock)\n", dn, in);
  1868. iput(in);
  1869. skipped++;
  1870. goto next_item;
  1871. }
  1872. err = splice_dentry(&dn, in);
  1873. if (err < 0)
  1874. goto next_item;
  1875. }
  1876. ceph_dentry(dn)->offset = rde->offset;
  1877. update_dentry_lease(d_inode(parent), dn,
  1878. rde->lease, req->r_session,
  1879. req->r_request_started);
  1880. if (err == 0 && skipped == 0 && cache_ctl.index >= 0) {
  1881. ret = fill_readdir_cache(d_inode(parent), dn,
  1882. &cache_ctl, req);
  1883. if (ret < 0)
  1884. err = ret;
  1885. }
  1886. next_item:
  1887. dput(dn);
  1888. }
  1889. out:
  1890. if (err == 0 && skipped == 0) {
  1891. set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags);
  1892. req->r_readdir_cache_idx = cache_ctl.index;
  1893. }
  1894. ceph_readdir_cache_release(&cache_ctl);
  1895. doutc(cl, "done\n");
  1896. return err;
  1897. }
  1898. bool ceph_inode_set_size(struct inode *inode, loff_t size)
  1899. {
  1900. struct ceph_client *cl = ceph_inode_to_client(inode);
  1901. struct ceph_inode_info *ci = ceph_inode(inode);
  1902. bool ret;
  1903. spin_lock(&ci->i_ceph_lock);
  1904. doutc(cl, "set_size %p %llu -> %llu\n", inode, i_size_read(inode), size);
  1905. i_size_write(inode, size);
  1906. ceph_fscache_update(inode);
  1907. inode->i_blocks = calc_inode_blocks(size);
  1908. ret = __ceph_should_report_size(ci);
  1909. spin_unlock(&ci->i_ceph_lock);
  1910. return ret;
  1911. }
  1912. void ceph_queue_inode_work(struct inode *inode, int work_bit)
  1913. {
  1914. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
  1915. struct ceph_client *cl = fsc->client;
  1916. struct ceph_inode_info *ci = ceph_inode(inode);
  1917. set_bit(work_bit, &ci->i_work_mask);
  1918. ihold(inode);
  1919. if (queue_work(fsc->inode_wq, &ci->i_work)) {
  1920. doutc(cl, "%p %llx.%llx mask=%lx\n", inode,
  1921. ceph_vinop(inode), ci->i_work_mask);
  1922. } else {
  1923. doutc(cl, "%p %llx.%llx already queued, mask=%lx\n",
  1924. inode, ceph_vinop(inode), ci->i_work_mask);
  1925. iput(inode);
  1926. }
  1927. }
  1928. static void ceph_do_invalidate_pages(struct inode *inode)
  1929. {
  1930. struct ceph_client *cl = ceph_inode_to_client(inode);
  1931. struct ceph_inode_info *ci = ceph_inode(inode);
  1932. u32 orig_gen;
  1933. int check = 0;
  1934. ceph_fscache_invalidate(inode, false);
  1935. mutex_lock(&ci->i_truncate_mutex);
  1936. if (ceph_inode_is_shutdown(inode)) {
  1937. pr_warn_ratelimited_client(cl,
  1938. "%p %llx.%llx is shut down\n", inode,
  1939. ceph_vinop(inode));
  1940. mapping_set_error(inode->i_mapping, -EIO);
  1941. truncate_pagecache(inode, 0);
  1942. mutex_unlock(&ci->i_truncate_mutex);
  1943. goto out;
  1944. }
  1945. spin_lock(&ci->i_ceph_lock);
  1946. doutc(cl, "%p %llx.%llx gen %d revoking %d\n", inode,
  1947. ceph_vinop(inode), ci->i_rdcache_gen, ci->i_rdcache_revoking);
  1948. if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
  1949. if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
  1950. check = 1;
  1951. spin_unlock(&ci->i_ceph_lock);
  1952. mutex_unlock(&ci->i_truncate_mutex);
  1953. goto out;
  1954. }
  1955. orig_gen = ci->i_rdcache_gen;
  1956. spin_unlock(&ci->i_ceph_lock);
  1957. if (invalidate_inode_pages2(inode->i_mapping) < 0) {
  1958. pr_err_client(cl, "invalidate_inode_pages2 %llx.%llx failed\n",
  1959. ceph_vinop(inode));
  1960. }
  1961. spin_lock(&ci->i_ceph_lock);
  1962. if (orig_gen == ci->i_rdcache_gen &&
  1963. orig_gen == ci->i_rdcache_revoking) {
  1964. doutc(cl, "%p %llx.%llx gen %d successful\n", inode,
  1965. ceph_vinop(inode), ci->i_rdcache_gen);
  1966. ci->i_rdcache_revoking--;
  1967. check = 1;
  1968. } else {
  1969. doutc(cl, "%p %llx.%llx gen %d raced, now %d revoking %d\n",
  1970. inode, ceph_vinop(inode), orig_gen, ci->i_rdcache_gen,
  1971. ci->i_rdcache_revoking);
  1972. if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
  1973. check = 1;
  1974. }
  1975. spin_unlock(&ci->i_ceph_lock);
  1976. mutex_unlock(&ci->i_truncate_mutex);
  1977. out:
  1978. if (check)
  1979. ceph_check_caps(ci, 0);
  1980. }
  1981. /*
  1982. * Make sure any pending truncation is applied before doing anything
  1983. * that may depend on it.
  1984. */
  1985. void __ceph_do_pending_vmtruncate(struct inode *inode)
  1986. {
  1987. struct ceph_client *cl = ceph_inode_to_client(inode);
  1988. struct ceph_inode_info *ci = ceph_inode(inode);
  1989. u64 to;
  1990. int wrbuffer_refs, finish = 0;
  1991. mutex_lock(&ci->i_truncate_mutex);
  1992. retry:
  1993. spin_lock(&ci->i_ceph_lock);
  1994. if (ci->i_truncate_pending == 0) {
  1995. doutc(cl, "%p %llx.%llx none pending\n", inode,
  1996. ceph_vinop(inode));
  1997. spin_unlock(&ci->i_ceph_lock);
  1998. mutex_unlock(&ci->i_truncate_mutex);
  1999. return;
  2000. }
  2001. /*
  2002. * make sure any dirty snapped pages are flushed before we
  2003. * possibly truncate them.. so write AND block!
  2004. */
  2005. if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
  2006. spin_unlock(&ci->i_ceph_lock);
  2007. doutc(cl, "%p %llx.%llx flushing snaps first\n", inode,
  2008. ceph_vinop(inode));
  2009. filemap_write_and_wait_range(&inode->i_data, 0,
  2010. inode->i_sb->s_maxbytes);
  2011. goto retry;
  2012. }
  2013. /* there should be no reader or writer */
  2014. WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref);
  2015. to = ci->i_truncate_pagecache_size;
  2016. wrbuffer_refs = ci->i_wrbuffer_ref;
  2017. doutc(cl, "%p %llx.%llx (%d) to %lld\n", inode, ceph_vinop(inode),
  2018. ci->i_truncate_pending, to);
  2019. spin_unlock(&ci->i_ceph_lock);
  2020. ceph_fscache_resize(inode, to);
  2021. truncate_pagecache(inode, to);
  2022. spin_lock(&ci->i_ceph_lock);
  2023. if (to == ci->i_truncate_pagecache_size) {
  2024. ci->i_truncate_pending = 0;
  2025. finish = 1;
  2026. }
  2027. spin_unlock(&ci->i_ceph_lock);
  2028. if (!finish)
  2029. goto retry;
  2030. mutex_unlock(&ci->i_truncate_mutex);
  2031. if (wrbuffer_refs == 0)
  2032. ceph_check_caps(ci, 0);
  2033. wake_up_all(&ci->i_cap_wq);
  2034. }
  2035. static void ceph_inode_work(struct work_struct *work)
  2036. {
  2037. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  2038. i_work);
  2039. struct inode *inode = &ci->netfs.inode;
  2040. struct ceph_client *cl = ceph_inode_to_client(inode);
  2041. if (test_and_clear_bit(CEPH_I_WORK_WRITEBACK, &ci->i_work_mask)) {
  2042. doutc(cl, "writeback %p %llx.%llx\n", inode, ceph_vinop(inode));
  2043. filemap_fdatawrite(&inode->i_data);
  2044. }
  2045. if (test_and_clear_bit(CEPH_I_WORK_INVALIDATE_PAGES, &ci->i_work_mask))
  2046. ceph_do_invalidate_pages(inode);
  2047. if (test_and_clear_bit(CEPH_I_WORK_VMTRUNCATE, &ci->i_work_mask))
  2048. __ceph_do_pending_vmtruncate(inode);
  2049. if (test_and_clear_bit(CEPH_I_WORK_CHECK_CAPS, &ci->i_work_mask))
  2050. ceph_check_caps(ci, 0);
  2051. if (test_and_clear_bit(CEPH_I_WORK_FLUSH_SNAPS, &ci->i_work_mask))
  2052. ceph_flush_snaps(ci, NULL);
  2053. iput(inode);
  2054. }
  2055. static const char *ceph_encrypted_get_link(struct dentry *dentry,
  2056. struct inode *inode,
  2057. struct delayed_call *done)
  2058. {
  2059. struct ceph_inode_info *ci = ceph_inode(inode);
  2060. if (!dentry)
  2061. return ERR_PTR(-ECHILD);
  2062. return fscrypt_get_symlink(inode, ci->i_symlink, i_size_read(inode),
  2063. done);
  2064. }
  2065. static int ceph_encrypted_symlink_getattr(struct mnt_idmap *idmap,
  2066. const struct path *path,
  2067. struct kstat *stat, u32 request_mask,
  2068. unsigned int query_flags)
  2069. {
  2070. int ret;
  2071. ret = ceph_getattr(idmap, path, stat, request_mask, query_flags);
  2072. if (ret)
  2073. return ret;
  2074. return fscrypt_symlink_getattr(path, stat);
  2075. }
  2076. /*
  2077. * symlinks
  2078. */
  2079. static const struct inode_operations ceph_symlink_iops = {
  2080. .get_link = simple_get_link,
  2081. .setattr = ceph_setattr,
  2082. .getattr = ceph_getattr,
  2083. .listxattr = ceph_listxattr,
  2084. };
  2085. static const struct inode_operations ceph_encrypted_symlink_iops = {
  2086. .get_link = ceph_encrypted_get_link,
  2087. .setattr = ceph_setattr,
  2088. .getattr = ceph_encrypted_symlink_getattr,
  2089. .listxattr = ceph_listxattr,
  2090. };
  2091. /*
  2092. * Transfer the encrypted last block to the MDS and the MDS
  2093. * will help update it when truncating a smaller size.
  2094. *
  2095. * We don't support a PAGE_SIZE that is smaller than the
  2096. * CEPH_FSCRYPT_BLOCK_SIZE.
  2097. */
  2098. static int fill_fscrypt_truncate(struct inode *inode,
  2099. struct ceph_mds_request *req,
  2100. struct iattr *attr)
  2101. {
  2102. struct ceph_client *cl = ceph_inode_to_client(inode);
  2103. struct ceph_inode_info *ci = ceph_inode(inode);
  2104. int boff = attr->ia_size % CEPH_FSCRYPT_BLOCK_SIZE;
  2105. loff_t pos, orig_pos = round_down(attr->ia_size,
  2106. CEPH_FSCRYPT_BLOCK_SIZE);
  2107. u64 block = orig_pos >> CEPH_FSCRYPT_BLOCK_SHIFT;
  2108. struct ceph_pagelist *pagelist = NULL;
  2109. struct kvec iov = {0};
  2110. struct iov_iter iter;
  2111. struct page *page = NULL;
  2112. struct ceph_fscrypt_truncate_size_header header;
  2113. int retry_op = 0;
  2114. int len = CEPH_FSCRYPT_BLOCK_SIZE;
  2115. loff_t i_size = i_size_read(inode);
  2116. int got, ret, issued;
  2117. u64 objver;
  2118. ret = __ceph_get_caps(inode, NULL, CEPH_CAP_FILE_RD, 0, -1, &got);
  2119. if (ret < 0)
  2120. return ret;
  2121. issued = __ceph_caps_issued(ci, NULL);
  2122. doutc(cl, "size %lld -> %lld got cap refs on %s, issued %s\n",
  2123. i_size, attr->ia_size, ceph_cap_string(got),
  2124. ceph_cap_string(issued));
  2125. /* Try to writeback the dirty pagecaches */
  2126. if (issued & (CEPH_CAP_FILE_BUFFER)) {
  2127. loff_t lend = orig_pos + CEPH_FSCRYPT_BLOCK_SIZE - 1;
  2128. ret = filemap_write_and_wait_range(inode->i_mapping,
  2129. orig_pos, lend);
  2130. if (ret < 0)
  2131. goto out;
  2132. }
  2133. page = __page_cache_alloc(GFP_KERNEL);
  2134. if (page == NULL) {
  2135. ret = -ENOMEM;
  2136. goto out;
  2137. }
  2138. pagelist = ceph_pagelist_alloc(GFP_KERNEL);
  2139. if (!pagelist) {
  2140. ret = -ENOMEM;
  2141. goto out;
  2142. }
  2143. iov.iov_base = kmap_local_page(page);
  2144. iov.iov_len = len;
  2145. iov_iter_kvec(&iter, READ, &iov, 1, len);
  2146. pos = orig_pos;
  2147. ret = __ceph_sync_read(inode, &pos, &iter, &retry_op, &objver);
  2148. if (ret < 0)
  2149. goto out;
  2150. /* Insert the header first */
  2151. header.ver = 1;
  2152. header.compat = 1;
  2153. header.change_attr = cpu_to_le64(inode_peek_iversion_raw(inode));
  2154. /*
  2155. * Always set the block_size to CEPH_FSCRYPT_BLOCK_SIZE,
  2156. * because in MDS it may need this to do the truncate.
  2157. */
  2158. header.block_size = cpu_to_le32(CEPH_FSCRYPT_BLOCK_SIZE);
  2159. /*
  2160. * If we hit a hole here, we should just skip filling
  2161. * the fscrypt for the request, because once the fscrypt
  2162. * is enabled, the file will be split into many blocks
  2163. * with the size of CEPH_FSCRYPT_BLOCK_SIZE, if there
  2164. * has a hole, the hole size should be multiple of block
  2165. * size.
  2166. *
  2167. * If the Rados object doesn't exist, it will be set to 0.
  2168. */
  2169. if (!objver) {
  2170. doutc(cl, "hit hole, ppos %lld < size %lld\n", pos, i_size);
  2171. header.data_len = cpu_to_le32(8 + 8 + 4);
  2172. header.file_offset = 0;
  2173. ret = 0;
  2174. } else {
  2175. header.data_len = cpu_to_le32(8 + 8 + 4 + CEPH_FSCRYPT_BLOCK_SIZE);
  2176. header.file_offset = cpu_to_le64(orig_pos);
  2177. doutc(cl, "encrypt block boff/bsize %d/%lu\n", boff,
  2178. CEPH_FSCRYPT_BLOCK_SIZE);
  2179. /* truncate and zero out the extra contents for the last block */
  2180. memset(iov.iov_base + boff, 0, PAGE_SIZE - boff);
  2181. /* encrypt the last block */
  2182. ret = ceph_fscrypt_encrypt_block_inplace(inode, page,
  2183. CEPH_FSCRYPT_BLOCK_SIZE,
  2184. 0, block,
  2185. GFP_KERNEL);
  2186. if (ret)
  2187. goto out;
  2188. }
  2189. /* Insert the header */
  2190. ret = ceph_pagelist_append(pagelist, &header, sizeof(header));
  2191. if (ret)
  2192. goto out;
  2193. if (header.block_size) {
  2194. /* Append the last block contents to pagelist */
  2195. ret = ceph_pagelist_append(pagelist, iov.iov_base,
  2196. CEPH_FSCRYPT_BLOCK_SIZE);
  2197. if (ret)
  2198. goto out;
  2199. }
  2200. req->r_pagelist = pagelist;
  2201. out:
  2202. doutc(cl, "%p %llx.%llx size dropping cap refs on %s\n", inode,
  2203. ceph_vinop(inode), ceph_cap_string(got));
  2204. ceph_put_cap_refs(ci, got);
  2205. if (iov.iov_base)
  2206. kunmap_local(iov.iov_base);
  2207. if (page)
  2208. __free_pages(page, 0);
  2209. if (ret && pagelist)
  2210. ceph_pagelist_release(pagelist);
  2211. return ret;
  2212. }
  2213. int __ceph_setattr(struct mnt_idmap *idmap, struct inode *inode,
  2214. struct iattr *attr, struct ceph_iattr *cia)
  2215. {
  2216. struct ceph_inode_info *ci = ceph_inode(inode);
  2217. unsigned int ia_valid = attr->ia_valid;
  2218. struct ceph_mds_request *req;
  2219. struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc;
  2220. struct ceph_client *cl = ceph_inode_to_client(inode);
  2221. struct ceph_cap_flush *prealloc_cf;
  2222. loff_t isize = i_size_read(inode);
  2223. int issued;
  2224. int release = 0, dirtied = 0;
  2225. int mask = 0;
  2226. int err = 0;
  2227. int inode_dirty_flags = 0;
  2228. bool lock_snap_rwsem = false;
  2229. bool fill_fscrypt;
  2230. int truncate_retry = 20; /* The RMW will take around 50ms */
  2231. struct dentry *dentry;
  2232. char *path;
  2233. bool do_sync = false;
  2234. dentry = d_find_alias(inode);
  2235. if (!dentry) {
  2236. do_sync = true;
  2237. } else {
  2238. struct ceph_path_info path_info;
  2239. path = ceph_mdsc_build_path(mdsc, dentry, &path_info, 0);
  2240. if (IS_ERR(path)) {
  2241. do_sync = true;
  2242. err = 0;
  2243. } else {
  2244. err = ceph_mds_check_access(mdsc, path, MAY_WRITE);
  2245. }
  2246. ceph_mdsc_free_path_info(&path_info);
  2247. dput(dentry);
  2248. /* For none EACCES cases will let the MDS do the mds auth check */
  2249. if (err == -EACCES) {
  2250. return err;
  2251. } else if (err < 0) {
  2252. do_sync = true;
  2253. err = 0;
  2254. }
  2255. }
  2256. retry:
  2257. prealloc_cf = ceph_alloc_cap_flush();
  2258. if (!prealloc_cf)
  2259. return -ENOMEM;
  2260. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
  2261. USE_AUTH_MDS);
  2262. if (IS_ERR(req)) {
  2263. ceph_free_cap_flush(prealloc_cf);
  2264. return PTR_ERR(req);
  2265. }
  2266. fill_fscrypt = false;
  2267. spin_lock(&ci->i_ceph_lock);
  2268. issued = __ceph_caps_issued(ci, NULL);
  2269. if (!ci->i_head_snapc &&
  2270. (issued & (CEPH_CAP_ANY_EXCL | CEPH_CAP_FILE_WR))) {
  2271. lock_snap_rwsem = true;
  2272. if (!down_read_trylock(&mdsc->snap_rwsem)) {
  2273. spin_unlock(&ci->i_ceph_lock);
  2274. down_read(&mdsc->snap_rwsem);
  2275. spin_lock(&ci->i_ceph_lock);
  2276. issued = __ceph_caps_issued(ci, NULL);
  2277. }
  2278. }
  2279. doutc(cl, "%p %llx.%llx issued %s\n", inode, ceph_vinop(inode),
  2280. ceph_cap_string(issued));
  2281. #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
  2282. if (cia && cia->fscrypt_auth) {
  2283. u32 len = ceph_fscrypt_auth_len(cia->fscrypt_auth);
  2284. if (len > sizeof(*cia->fscrypt_auth)) {
  2285. err = -EINVAL;
  2286. spin_unlock(&ci->i_ceph_lock);
  2287. goto out;
  2288. }
  2289. doutc(cl, "%p %llx.%llx fscrypt_auth len %u to %u)\n", inode,
  2290. ceph_vinop(inode), ci->fscrypt_auth_len, len);
  2291. /* It should never be re-set once set */
  2292. WARN_ON_ONCE(ci->fscrypt_auth);
  2293. if (!do_sync && (issued & CEPH_CAP_AUTH_EXCL)) {
  2294. dirtied |= CEPH_CAP_AUTH_EXCL;
  2295. kfree(ci->fscrypt_auth);
  2296. ci->fscrypt_auth = (u8 *)cia->fscrypt_auth;
  2297. ci->fscrypt_auth_len = len;
  2298. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  2299. ci->fscrypt_auth_len != len ||
  2300. memcmp(ci->fscrypt_auth, cia->fscrypt_auth, len)) {
  2301. req->r_fscrypt_auth = cia->fscrypt_auth;
  2302. mask |= CEPH_SETATTR_FSCRYPT_AUTH;
  2303. release |= CEPH_CAP_AUTH_SHARED;
  2304. }
  2305. cia->fscrypt_auth = NULL;
  2306. }
  2307. #else
  2308. if (cia && cia->fscrypt_auth) {
  2309. err = -EINVAL;
  2310. spin_unlock(&ci->i_ceph_lock);
  2311. goto out;
  2312. }
  2313. #endif /* CONFIG_FS_ENCRYPTION */
  2314. if (ia_valid & ATTR_UID) {
  2315. kuid_t fsuid = from_vfsuid(idmap, i_user_ns(inode), attr->ia_vfsuid);
  2316. doutc(cl, "%p %llx.%llx uid %d -> %d\n", inode,
  2317. ceph_vinop(inode),
  2318. from_kuid(&init_user_ns, inode->i_uid),
  2319. from_kuid(&init_user_ns, attr->ia_uid));
  2320. if (!do_sync && (issued & CEPH_CAP_AUTH_EXCL)) {
  2321. inode->i_uid = fsuid;
  2322. dirtied |= CEPH_CAP_AUTH_EXCL;
  2323. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  2324. !uid_eq(fsuid, inode->i_uid)) {
  2325. req->r_args.setattr.uid = cpu_to_le32(
  2326. from_kuid(&init_user_ns, fsuid));
  2327. mask |= CEPH_SETATTR_UID;
  2328. release |= CEPH_CAP_AUTH_SHARED;
  2329. }
  2330. }
  2331. if (ia_valid & ATTR_GID) {
  2332. kgid_t fsgid = from_vfsgid(idmap, i_user_ns(inode), attr->ia_vfsgid);
  2333. doutc(cl, "%p %llx.%llx gid %d -> %d\n", inode,
  2334. ceph_vinop(inode),
  2335. from_kgid(&init_user_ns, inode->i_gid),
  2336. from_kgid(&init_user_ns, attr->ia_gid));
  2337. if (!do_sync && (issued & CEPH_CAP_AUTH_EXCL)) {
  2338. inode->i_gid = fsgid;
  2339. dirtied |= CEPH_CAP_AUTH_EXCL;
  2340. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  2341. !gid_eq(fsgid, inode->i_gid)) {
  2342. req->r_args.setattr.gid = cpu_to_le32(
  2343. from_kgid(&init_user_ns, fsgid));
  2344. mask |= CEPH_SETATTR_GID;
  2345. release |= CEPH_CAP_AUTH_SHARED;
  2346. }
  2347. }
  2348. if (ia_valid & ATTR_MODE) {
  2349. doutc(cl, "%p %llx.%llx mode 0%o -> 0%o\n", inode,
  2350. ceph_vinop(inode), inode->i_mode, attr->ia_mode);
  2351. if (!do_sync && (issued & CEPH_CAP_AUTH_EXCL)) {
  2352. inode->i_mode = attr->ia_mode;
  2353. dirtied |= CEPH_CAP_AUTH_EXCL;
  2354. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  2355. attr->ia_mode != inode->i_mode) {
  2356. inode->i_mode = attr->ia_mode;
  2357. req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
  2358. mask |= CEPH_SETATTR_MODE;
  2359. release |= CEPH_CAP_AUTH_SHARED;
  2360. }
  2361. }
  2362. if (ia_valid & ATTR_ATIME) {
  2363. struct timespec64 atime = inode_get_atime(inode);
  2364. doutc(cl, "%p %llx.%llx atime %lld.%09ld -> %lld.%09ld\n",
  2365. inode, ceph_vinop(inode),
  2366. atime.tv_sec, atime.tv_nsec,
  2367. attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
  2368. if (!do_sync && (issued & CEPH_CAP_FILE_EXCL)) {
  2369. ci->i_time_warp_seq++;
  2370. inode_set_atime_to_ts(inode, attr->ia_atime);
  2371. dirtied |= CEPH_CAP_FILE_EXCL;
  2372. } else if (!do_sync && (issued & CEPH_CAP_FILE_WR) &&
  2373. timespec64_compare(&atime,
  2374. &attr->ia_atime) < 0) {
  2375. inode_set_atime_to_ts(inode, attr->ia_atime);
  2376. dirtied |= CEPH_CAP_FILE_WR;
  2377. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  2378. !timespec64_equal(&atime, &attr->ia_atime)) {
  2379. ceph_encode_timespec64(&req->r_args.setattr.atime,
  2380. &attr->ia_atime);
  2381. mask |= CEPH_SETATTR_ATIME;
  2382. release |= CEPH_CAP_FILE_SHARED |
  2383. CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
  2384. }
  2385. }
  2386. if (ia_valid & ATTR_SIZE) {
  2387. doutc(cl, "%p %llx.%llx size %lld -> %lld\n", inode,
  2388. ceph_vinop(inode), isize, attr->ia_size);
  2389. /*
  2390. * Only when the new size is smaller and not aligned to
  2391. * CEPH_FSCRYPT_BLOCK_SIZE will the RMW is needed.
  2392. */
  2393. if (IS_ENCRYPTED(inode) && attr->ia_size < isize &&
  2394. (attr->ia_size % CEPH_FSCRYPT_BLOCK_SIZE)) {
  2395. mask |= CEPH_SETATTR_SIZE;
  2396. release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL |
  2397. CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
  2398. set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
  2399. mask |= CEPH_SETATTR_FSCRYPT_FILE;
  2400. req->r_args.setattr.size =
  2401. cpu_to_le64(round_up(attr->ia_size,
  2402. CEPH_FSCRYPT_BLOCK_SIZE));
  2403. req->r_args.setattr.old_size =
  2404. cpu_to_le64(round_up(isize,
  2405. CEPH_FSCRYPT_BLOCK_SIZE));
  2406. req->r_fscrypt_file = attr->ia_size;
  2407. fill_fscrypt = true;
  2408. } else if (!do_sync && (issued & CEPH_CAP_FILE_EXCL) && attr->ia_size >= isize) {
  2409. if (attr->ia_size > isize) {
  2410. i_size_write(inode, attr->ia_size);
  2411. inode->i_blocks = calc_inode_blocks(attr->ia_size);
  2412. ci->i_reported_size = attr->ia_size;
  2413. dirtied |= CEPH_CAP_FILE_EXCL;
  2414. ia_valid |= ATTR_MTIME;
  2415. }
  2416. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  2417. attr->ia_size != isize) {
  2418. mask |= CEPH_SETATTR_SIZE;
  2419. release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL |
  2420. CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
  2421. if (IS_ENCRYPTED(inode) && attr->ia_size) {
  2422. set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
  2423. mask |= CEPH_SETATTR_FSCRYPT_FILE;
  2424. req->r_args.setattr.size =
  2425. cpu_to_le64(round_up(attr->ia_size,
  2426. CEPH_FSCRYPT_BLOCK_SIZE));
  2427. req->r_args.setattr.old_size =
  2428. cpu_to_le64(round_up(isize,
  2429. CEPH_FSCRYPT_BLOCK_SIZE));
  2430. req->r_fscrypt_file = attr->ia_size;
  2431. } else {
  2432. req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
  2433. req->r_args.setattr.old_size = cpu_to_le64(isize);
  2434. req->r_fscrypt_file = 0;
  2435. }
  2436. }
  2437. }
  2438. if (ia_valid & ATTR_MTIME) {
  2439. struct timespec64 mtime = inode_get_mtime(inode);
  2440. doutc(cl, "%p %llx.%llx mtime %lld.%09ld -> %lld.%09ld\n",
  2441. inode, ceph_vinop(inode),
  2442. mtime.tv_sec, mtime.tv_nsec,
  2443. attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
  2444. if (!do_sync && (issued & CEPH_CAP_FILE_EXCL)) {
  2445. ci->i_time_warp_seq++;
  2446. inode_set_mtime_to_ts(inode, attr->ia_mtime);
  2447. dirtied |= CEPH_CAP_FILE_EXCL;
  2448. } else if (!do_sync && (issued & CEPH_CAP_FILE_WR) &&
  2449. timespec64_compare(&mtime, &attr->ia_mtime) < 0) {
  2450. inode_set_mtime_to_ts(inode, attr->ia_mtime);
  2451. dirtied |= CEPH_CAP_FILE_WR;
  2452. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  2453. !timespec64_equal(&mtime, &attr->ia_mtime)) {
  2454. ceph_encode_timespec64(&req->r_args.setattr.mtime,
  2455. &attr->ia_mtime);
  2456. mask |= CEPH_SETATTR_MTIME;
  2457. release |= CEPH_CAP_FILE_SHARED |
  2458. CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
  2459. }
  2460. }
  2461. /* these do nothing */
  2462. if (ia_valid & ATTR_CTIME) {
  2463. bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
  2464. ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
  2465. doutc(cl, "%p %llx.%llx ctime %lld.%09ld -> %lld.%09ld (%s)\n",
  2466. inode, ceph_vinop(inode),
  2467. inode_get_ctime_sec(inode),
  2468. inode_get_ctime_nsec(inode),
  2469. attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
  2470. only ? "ctime only" : "ignored");
  2471. if (only) {
  2472. /*
  2473. * if kernel wants to dirty ctime but nothing else,
  2474. * we need to choose a cap to dirty under, or do
  2475. * a almost-no-op setattr
  2476. */
  2477. if (issued & CEPH_CAP_AUTH_EXCL)
  2478. dirtied |= CEPH_CAP_AUTH_EXCL;
  2479. else if (issued & CEPH_CAP_FILE_EXCL)
  2480. dirtied |= CEPH_CAP_FILE_EXCL;
  2481. else if (issued & CEPH_CAP_XATTR_EXCL)
  2482. dirtied |= CEPH_CAP_XATTR_EXCL;
  2483. else
  2484. mask |= CEPH_SETATTR_CTIME;
  2485. }
  2486. }
  2487. if (ia_valid & ATTR_FILE)
  2488. doutc(cl, "%p %llx.%llx ATTR_FILE ... hrm!\n", inode,
  2489. ceph_vinop(inode));
  2490. if (dirtied) {
  2491. inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied,
  2492. &prealloc_cf);
  2493. inode_set_ctime_to_ts(inode, attr->ia_ctime);
  2494. inode_inc_iversion_raw(inode);
  2495. }
  2496. release &= issued;
  2497. spin_unlock(&ci->i_ceph_lock);
  2498. if (lock_snap_rwsem) {
  2499. up_read(&mdsc->snap_rwsem);
  2500. lock_snap_rwsem = false;
  2501. }
  2502. if (inode_dirty_flags)
  2503. __mark_inode_dirty(inode, inode_dirty_flags);
  2504. if (mask) {
  2505. req->r_inode = inode;
  2506. ihold(inode);
  2507. req->r_inode_drop = release;
  2508. req->r_args.setattr.mask = cpu_to_le32(mask);
  2509. req->r_num_caps = 1;
  2510. req->r_stamp = attr->ia_ctime;
  2511. if (fill_fscrypt) {
  2512. err = fill_fscrypt_truncate(inode, req, attr);
  2513. if (err)
  2514. goto out;
  2515. }
  2516. /*
  2517. * The truncate request will return -EAGAIN when the
  2518. * last block has been updated just before the MDS
  2519. * successfully gets the xlock for the FILE lock. To
  2520. * avoid corrupting the file contents we need to retry
  2521. * it.
  2522. */
  2523. err = ceph_mdsc_do_request(mdsc, NULL, req);
  2524. if (err == -EAGAIN && truncate_retry--) {
  2525. doutc(cl, "%p %llx.%llx result=%d (%s locally, %d remote), retry it!\n",
  2526. inode, ceph_vinop(inode), err,
  2527. ceph_cap_string(dirtied), mask);
  2528. ceph_mdsc_put_request(req);
  2529. ceph_free_cap_flush(prealloc_cf);
  2530. goto retry;
  2531. }
  2532. }
  2533. out:
  2534. doutc(cl, "%p %llx.%llx result=%d (%s locally, %d remote)\n", inode,
  2535. ceph_vinop(inode), err, ceph_cap_string(dirtied), mask);
  2536. ceph_mdsc_put_request(req);
  2537. ceph_free_cap_flush(prealloc_cf);
  2538. if (err >= 0 && (mask & CEPH_SETATTR_SIZE))
  2539. __ceph_do_pending_vmtruncate(inode);
  2540. return err;
  2541. }
  2542. /*
  2543. * setattr
  2544. */
  2545. int ceph_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
  2546. struct iattr *attr)
  2547. {
  2548. struct inode *inode = d_inode(dentry);
  2549. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
  2550. int err;
  2551. if (ceph_snap(inode) != CEPH_NOSNAP)
  2552. return -EROFS;
  2553. if (ceph_inode_is_shutdown(inode))
  2554. return -ESTALE;
  2555. err = fscrypt_prepare_setattr(dentry, attr);
  2556. if (err)
  2557. return err;
  2558. err = setattr_prepare(idmap, dentry, attr);
  2559. if (err != 0)
  2560. return err;
  2561. if ((attr->ia_valid & ATTR_SIZE) &&
  2562. attr->ia_size > max(i_size_read(inode), fsc->max_file_size))
  2563. return -EFBIG;
  2564. if ((attr->ia_valid & ATTR_SIZE) &&
  2565. ceph_quota_is_max_bytes_exceeded(inode, attr->ia_size))
  2566. return -EDQUOT;
  2567. err = __ceph_setattr(idmap, inode, attr, NULL);
  2568. if (err >= 0 && (attr->ia_valid & ATTR_MODE))
  2569. err = posix_acl_chmod(idmap, dentry, attr->ia_mode);
  2570. return err;
  2571. }
  2572. int ceph_try_to_choose_auth_mds(struct inode *inode, int mask)
  2573. {
  2574. int issued = ceph_caps_issued(ceph_inode(inode));
  2575. /*
  2576. * If any 'x' caps is issued we can just choose the auth MDS
  2577. * instead of the random replica MDSes. Because only when the
  2578. * Locker is in LOCK_EXEC state will the loner client could
  2579. * get the 'x' caps. And if we send the getattr requests to
  2580. * any replica MDS it must auth pin and tries to rdlock from
  2581. * the auth MDS, and then the auth MDS need to do the Locker
  2582. * state transition to LOCK_SYNC. And after that the lock state
  2583. * will change back.
  2584. *
  2585. * This cost much when doing the Locker state transition and
  2586. * usually will need to revoke caps from clients.
  2587. *
  2588. * And for the 'Xs' caps for getxattr we will also choose the
  2589. * auth MDS, because the MDS side code is buggy due to setxattr
  2590. * won't notify the replica MDSes when the values changed and
  2591. * the replica MDS will return the old values. Though we will
  2592. * fix it in MDS code, but this still makes sense for old ceph.
  2593. */
  2594. if (((mask & CEPH_CAP_ANY_SHARED) && (issued & CEPH_CAP_ANY_EXCL))
  2595. || (mask & (CEPH_STAT_RSTAT | CEPH_STAT_CAP_XATTR)))
  2596. return USE_AUTH_MDS;
  2597. else
  2598. return USE_ANY_MDS;
  2599. }
  2600. /*
  2601. * Verify that we have a lease on the given mask. If not,
  2602. * do a getattr against an mds.
  2603. */
  2604. int __ceph_do_getattr(struct inode *inode, struct page *locked_page,
  2605. int mask, bool force)
  2606. {
  2607. struct ceph_fs_client *fsc = ceph_sb_to_fs_client(inode->i_sb);
  2608. struct ceph_client *cl = fsc->client;
  2609. struct ceph_mds_client *mdsc = fsc->mdsc;
  2610. struct ceph_mds_request *req;
  2611. int mode;
  2612. int err;
  2613. if (ceph_snap(inode) == CEPH_SNAPDIR) {
  2614. doutc(cl, "inode %p %llx.%llx SNAPDIR\n", inode,
  2615. ceph_vinop(inode));
  2616. return 0;
  2617. }
  2618. doutc(cl, "inode %p %llx.%llx mask %s mode 0%o\n", inode,
  2619. ceph_vinop(inode), ceph_cap_string(mask), inode->i_mode);
  2620. if (!force && ceph_caps_issued_mask_metric(ceph_inode(inode), mask, 1))
  2621. return 0;
  2622. mode = ceph_try_to_choose_auth_mds(inode, mask);
  2623. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode);
  2624. if (IS_ERR(req))
  2625. return PTR_ERR(req);
  2626. req->r_inode = inode;
  2627. ihold(inode);
  2628. req->r_num_caps = 1;
  2629. req->r_args.getattr.mask = cpu_to_le32(mask);
  2630. req->r_locked_page = locked_page;
  2631. err = ceph_mdsc_do_request(mdsc, NULL, req);
  2632. if (locked_page && err == 0) {
  2633. u64 inline_version = req->r_reply_info.targeti.inline_version;
  2634. if (inline_version == 0) {
  2635. /* the reply is supposed to contain inline data */
  2636. err = -EINVAL;
  2637. } else if (inline_version == CEPH_INLINE_NONE ||
  2638. inline_version == 1) {
  2639. err = -ENODATA;
  2640. } else {
  2641. err = req->r_reply_info.targeti.inline_len;
  2642. }
  2643. }
  2644. ceph_mdsc_put_request(req);
  2645. doutc(cl, "result=%d\n", err);
  2646. return err;
  2647. }
  2648. int ceph_do_getvxattr(struct inode *inode, const char *name, void *value,
  2649. size_t size)
  2650. {
  2651. struct ceph_fs_client *fsc = ceph_sb_to_fs_client(inode->i_sb);
  2652. struct ceph_client *cl = fsc->client;
  2653. struct ceph_mds_client *mdsc = fsc->mdsc;
  2654. struct ceph_mds_request *req;
  2655. int mode = USE_AUTH_MDS;
  2656. int err;
  2657. char *xattr_value;
  2658. size_t xattr_value_len;
  2659. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETVXATTR, mode);
  2660. if (IS_ERR(req)) {
  2661. err = -ENOMEM;
  2662. goto out;
  2663. }
  2664. req->r_feature_needed = CEPHFS_FEATURE_OP_GETVXATTR;
  2665. req->r_path2 = kstrdup(name, GFP_NOFS);
  2666. if (!req->r_path2) {
  2667. err = -ENOMEM;
  2668. goto put;
  2669. }
  2670. ihold(inode);
  2671. req->r_inode = inode;
  2672. err = ceph_mdsc_do_request(mdsc, NULL, req);
  2673. if (err < 0)
  2674. goto put;
  2675. xattr_value = req->r_reply_info.xattr_info.xattr_value;
  2676. xattr_value_len = req->r_reply_info.xattr_info.xattr_value_len;
  2677. doutc(cl, "xattr_value_len:%zu, size:%zu\n", xattr_value_len, size);
  2678. err = (int)xattr_value_len;
  2679. if (size == 0)
  2680. goto put;
  2681. if (xattr_value_len > size) {
  2682. err = -ERANGE;
  2683. goto put;
  2684. }
  2685. memcpy(value, xattr_value, xattr_value_len);
  2686. put:
  2687. ceph_mdsc_put_request(req);
  2688. out:
  2689. doutc(cl, "result=%d\n", err);
  2690. return err;
  2691. }
  2692. /*
  2693. * Check inode permissions. We verify we have a valid value for
  2694. * the AUTH cap, then call the generic handler.
  2695. */
  2696. int ceph_permission(struct mnt_idmap *idmap, struct inode *inode,
  2697. int mask)
  2698. {
  2699. int err;
  2700. if (mask & MAY_NOT_BLOCK)
  2701. return -ECHILD;
  2702. err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, false);
  2703. if (!err)
  2704. err = generic_permission(idmap, inode, mask);
  2705. return err;
  2706. }
  2707. /* Craft a mask of needed caps given a set of requested statx attrs. */
  2708. static int statx_to_caps(u32 want, umode_t mode)
  2709. {
  2710. int mask = 0;
  2711. if (want & (STATX_MODE|STATX_UID|STATX_GID|STATX_CTIME|STATX_BTIME|STATX_CHANGE_COOKIE))
  2712. mask |= CEPH_CAP_AUTH_SHARED;
  2713. if (want & (STATX_NLINK|STATX_CTIME|STATX_CHANGE_COOKIE)) {
  2714. /*
  2715. * The link count for directories depends on inode->i_subdirs,
  2716. * and that is only updated when Fs caps are held.
  2717. */
  2718. if (S_ISDIR(mode))
  2719. mask |= CEPH_CAP_FILE_SHARED;
  2720. else
  2721. mask |= CEPH_CAP_LINK_SHARED;
  2722. }
  2723. if (want & (STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_SIZE|STATX_BLOCKS|STATX_CHANGE_COOKIE))
  2724. mask |= CEPH_CAP_FILE_SHARED;
  2725. if (want & (STATX_CTIME|STATX_CHANGE_COOKIE))
  2726. mask |= CEPH_CAP_XATTR_SHARED;
  2727. return mask;
  2728. }
  2729. /*
  2730. * Get all the attributes. If we have sufficient caps for the requested attrs,
  2731. * then we can avoid talking to the MDS at all.
  2732. */
  2733. int ceph_getattr(struct mnt_idmap *idmap, const struct path *path,
  2734. struct kstat *stat, u32 request_mask, unsigned int flags)
  2735. {
  2736. struct inode *inode = d_inode(path->dentry);
  2737. struct super_block *sb = inode->i_sb;
  2738. struct ceph_inode_info *ci = ceph_inode(inode);
  2739. u32 valid_mask = STATX_BASIC_STATS;
  2740. int err = 0;
  2741. if (ceph_inode_is_shutdown(inode))
  2742. return -ESTALE;
  2743. /* Skip the getattr altogether if we're asked not to sync */
  2744. if ((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) {
  2745. err = ceph_do_getattr(inode,
  2746. statx_to_caps(request_mask, inode->i_mode),
  2747. flags & AT_STATX_FORCE_SYNC);
  2748. if (err)
  2749. return err;
  2750. }
  2751. generic_fillattr(idmap, request_mask, inode, stat);
  2752. stat->ino = ceph_present_inode(inode);
  2753. /*
  2754. * btime on newly-allocated inodes is 0, so if this is still set to
  2755. * that, then assume that it's not valid.
  2756. */
  2757. if (ci->i_btime.tv_sec || ci->i_btime.tv_nsec) {
  2758. stat->btime = ci->i_btime;
  2759. valid_mask |= STATX_BTIME;
  2760. }
  2761. if (request_mask & STATX_CHANGE_COOKIE) {
  2762. stat->change_cookie = inode_peek_iversion_raw(inode);
  2763. valid_mask |= STATX_CHANGE_COOKIE;
  2764. }
  2765. if (ceph_snap(inode) == CEPH_NOSNAP)
  2766. stat->dev = sb->s_dev;
  2767. else
  2768. stat->dev = ci->i_snapid_map ? ci->i_snapid_map->dev : 0;
  2769. if (S_ISDIR(inode->i_mode)) {
  2770. if (ceph_test_mount_opt(ceph_sb_to_fs_client(sb), RBYTES)) {
  2771. stat->size = ci->i_rbytes;
  2772. } else if (ceph_snap(inode) == CEPH_SNAPDIR) {
  2773. struct ceph_inode_info *pci;
  2774. struct ceph_snap_realm *realm;
  2775. struct inode *parent;
  2776. parent = ceph_lookup_inode(sb, ceph_ino(inode));
  2777. if (IS_ERR(parent))
  2778. return PTR_ERR(parent);
  2779. pci = ceph_inode(parent);
  2780. spin_lock(&pci->i_ceph_lock);
  2781. realm = pci->i_snap_realm;
  2782. if (realm)
  2783. stat->size = realm->num_snaps;
  2784. else
  2785. stat->size = 0;
  2786. spin_unlock(&pci->i_ceph_lock);
  2787. iput(parent);
  2788. } else {
  2789. stat->size = ci->i_files + ci->i_subdirs;
  2790. }
  2791. stat->blocks = 0;
  2792. stat->blksize = 65536;
  2793. /*
  2794. * Some applications rely on the number of st_nlink
  2795. * value on directories to be either 0 (if unlinked)
  2796. * or 2 + number of subdirectories.
  2797. */
  2798. if (stat->nlink == 1)
  2799. /* '.' + '..' + subdirs */
  2800. stat->nlink = 1 + 1 + ci->i_subdirs;
  2801. }
  2802. stat->attributes |= STATX_ATTR_CHANGE_MONOTONIC;
  2803. if (IS_ENCRYPTED(inode))
  2804. stat->attributes |= STATX_ATTR_ENCRYPTED;
  2805. stat->attributes_mask |= (STATX_ATTR_CHANGE_MONOTONIC |
  2806. STATX_ATTR_ENCRYPTED);
  2807. stat->result_mask = request_mask & valid_mask;
  2808. return err;
  2809. }
  2810. void ceph_inode_shutdown(struct inode *inode)
  2811. {
  2812. struct ceph_inode_info *ci = ceph_inode(inode);
  2813. struct rb_node *p;
  2814. int iputs = 0;
  2815. bool invalidate = false;
  2816. spin_lock(&ci->i_ceph_lock);
  2817. ci->i_ceph_flags |= CEPH_I_SHUTDOWN;
  2818. p = rb_first(&ci->i_caps);
  2819. while (p) {
  2820. struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
  2821. p = rb_next(p);
  2822. iputs += ceph_purge_inode_cap(inode, cap, &invalidate);
  2823. }
  2824. spin_unlock(&ci->i_ceph_lock);
  2825. if (invalidate)
  2826. ceph_queue_invalidate(inode);
  2827. while (iputs--)
  2828. iput(inode);
  2829. }