dir.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* dir.c: AFS filesystem directory handling
  3. *
  4. * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/fs.h>
  9. #include <linux/namei.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/swap.h>
  12. #include <linux/ctype.h>
  13. #include <linux/sched.h>
  14. #include <linux/iversion.h>
  15. #include <linux/iov_iter.h>
  16. #include <linux/task_io_accounting_ops.h>
  17. #include "internal.h"
  18. #include "afs_fs.h"
  19. #include "xdr_fs.h"
  20. static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
  21. unsigned int flags);
  22. static int afs_dir_open(struct inode *inode, struct file *file);
  23. static int afs_readdir(struct file *file, struct dir_context *ctx);
  24. static int afs_d_revalidate(struct inode *dir, const struct qstr *name,
  25. struct dentry *dentry, unsigned int flags);
  26. static int afs_d_delete(const struct dentry *dentry);
  27. static void afs_d_iput(struct dentry *dentry, struct inode *inode);
  28. static bool afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
  29. loff_t fpos, u64 ino, unsigned dtype);
  30. static bool afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
  31. loff_t fpos, u64 ino, unsigned dtype);
  32. static int afs_create(struct mnt_idmap *idmap, struct inode *dir,
  33. struct dentry *dentry, umode_t mode, bool excl);
  34. static struct dentry *afs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
  35. struct dentry *dentry, umode_t mode);
  36. static int afs_rmdir(struct inode *dir, struct dentry *dentry);
  37. static int afs_unlink(struct inode *dir, struct dentry *dentry);
  38. static int afs_link(struct dentry *from, struct inode *dir,
  39. struct dentry *dentry);
  40. static int afs_symlink(struct mnt_idmap *idmap, struct inode *dir,
  41. struct dentry *dentry, const char *content);
  42. static int afs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
  43. struct dentry *old_dentry, struct inode *new_dir,
  44. struct dentry *new_dentry, unsigned int flags);
  45. const struct file_operations afs_dir_file_operations = {
  46. .open = afs_dir_open,
  47. .release = afs_release,
  48. .iterate_shared = afs_readdir,
  49. .lock = afs_lock,
  50. .llseek = generic_file_llseek,
  51. };
  52. const struct inode_operations afs_dir_inode_operations = {
  53. .create = afs_create,
  54. .lookup = afs_lookup,
  55. .link = afs_link,
  56. .unlink = afs_unlink,
  57. .symlink = afs_symlink,
  58. .mkdir = afs_mkdir,
  59. .rmdir = afs_rmdir,
  60. .rename = afs_rename,
  61. .permission = afs_permission,
  62. .getattr = afs_getattr,
  63. .setattr = afs_setattr,
  64. };
  65. const struct address_space_operations afs_dir_aops = {
  66. .writepages = afs_single_writepages,
  67. };
  68. const struct dentry_operations afs_fs_dentry_operations = {
  69. .d_revalidate = afs_d_revalidate,
  70. .d_delete = afs_d_delete,
  71. .d_release = afs_d_release,
  72. .d_automount = afs_d_automount,
  73. .d_iput = afs_d_iput,
  74. };
  75. struct afs_lookup_one_cookie {
  76. struct dir_context ctx;
  77. struct qstr name;
  78. bool found;
  79. struct afs_fid fid;
  80. };
  81. struct afs_lookup_cookie {
  82. struct dir_context ctx;
  83. struct qstr name;
  84. unsigned short nr_fids;
  85. struct afs_fid fids[50];
  86. };
  87. static void afs_dir_unuse_cookie(struct afs_vnode *dvnode, int ret)
  88. {
  89. if (ret == 0) {
  90. struct afs_vnode_cache_aux aux;
  91. loff_t i_size = i_size_read(&dvnode->netfs.inode);
  92. afs_set_cache_aux(dvnode, &aux);
  93. fscache_unuse_cookie(afs_vnode_cache(dvnode), &aux, &i_size);
  94. } else {
  95. fscache_unuse_cookie(afs_vnode_cache(dvnode), NULL, NULL);
  96. }
  97. }
  98. /*
  99. * Iterate through a kmapped directory segment, dumping a summary of
  100. * the contents.
  101. */
  102. static size_t afs_dir_dump_step(void *iter_base, size_t progress, size_t len,
  103. void *priv, void *priv2)
  104. {
  105. do {
  106. union afs_xdr_dir_block *block = iter_base;
  107. pr_warn("[%05zx] %32phN\n", progress, block);
  108. iter_base += AFS_DIR_BLOCK_SIZE;
  109. progress += AFS_DIR_BLOCK_SIZE;
  110. len -= AFS_DIR_BLOCK_SIZE;
  111. } while (len > 0);
  112. return len;
  113. }
  114. /*
  115. * Dump the contents of a directory.
  116. */
  117. static void afs_dir_dump(struct afs_vnode *dvnode)
  118. {
  119. struct iov_iter iter;
  120. unsigned long long i_size = i_size_read(&dvnode->netfs.inode);
  121. pr_warn("DIR %llx:%llx is=%llx\n",
  122. dvnode->fid.vid, dvnode->fid.vnode, i_size);
  123. iov_iter_folio_queue(&iter, ITER_SOURCE, dvnode->directory, 0, 0, i_size);
  124. iterate_folioq(&iter, iov_iter_count(&iter), NULL, NULL,
  125. afs_dir_dump_step);
  126. }
  127. /*
  128. * check that a directory folio is valid
  129. */
  130. static bool afs_dir_check_block(struct afs_vnode *dvnode, size_t progress,
  131. union afs_xdr_dir_block *block)
  132. {
  133. if (block->hdr.magic != AFS_DIR_MAGIC) {
  134. pr_warn("%s(%lx): [%zx] bad magic %04x\n",
  135. __func__, dvnode->netfs.inode.i_ino,
  136. progress, ntohs(block->hdr.magic));
  137. trace_afs_dir_check_failed(dvnode, progress);
  138. trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic);
  139. return false;
  140. }
  141. /* Make sure each block is NUL terminated so we can reasonably
  142. * use string functions on it. The filenames in the folio
  143. * *should* be NUL-terminated anyway.
  144. */
  145. ((u8 *)block)[AFS_DIR_BLOCK_SIZE - 1] = 0;
  146. afs_stat_v(dvnode, n_read_dir);
  147. return true;
  148. }
  149. /*
  150. * Iterate through a kmapped directory segment, checking the content.
  151. */
  152. static size_t afs_dir_check_step(void *iter_base, size_t progress, size_t len,
  153. void *priv, void *priv2)
  154. {
  155. struct afs_vnode *dvnode = priv;
  156. if (WARN_ON_ONCE(progress % AFS_DIR_BLOCK_SIZE ||
  157. len % AFS_DIR_BLOCK_SIZE))
  158. return len;
  159. do {
  160. if (!afs_dir_check_block(dvnode, progress, iter_base))
  161. break;
  162. iter_base += AFS_DIR_BLOCK_SIZE;
  163. len -= AFS_DIR_BLOCK_SIZE;
  164. } while (len > 0);
  165. return len;
  166. }
  167. /*
  168. * Check all the blocks in a directory.
  169. */
  170. static int afs_dir_check(struct afs_vnode *dvnode)
  171. {
  172. struct iov_iter iter;
  173. unsigned long long i_size = i_size_read(&dvnode->netfs.inode);
  174. size_t checked = 0;
  175. if (unlikely(!i_size))
  176. return 0;
  177. iov_iter_folio_queue(&iter, ITER_SOURCE, dvnode->directory, 0, 0, i_size);
  178. checked = iterate_folioq(&iter, iov_iter_count(&iter), dvnode, NULL,
  179. afs_dir_check_step);
  180. if (checked != i_size) {
  181. afs_dir_dump(dvnode);
  182. return -EIO;
  183. }
  184. return 0;
  185. }
  186. /*
  187. * open an AFS directory file
  188. */
  189. static int afs_dir_open(struct inode *inode, struct file *file)
  190. {
  191. _enter("{%lu}", inode->i_ino);
  192. BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
  193. BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
  194. if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
  195. return -ENOENT;
  196. return afs_open(inode, file);
  197. }
  198. /*
  199. * Read a file in a single download.
  200. */
  201. static ssize_t afs_do_read_single(struct afs_vnode *dvnode, struct file *file)
  202. {
  203. struct iov_iter iter;
  204. ssize_t ret;
  205. loff_t i_size;
  206. bool is_dir = (S_ISDIR(dvnode->netfs.inode.i_mode) &&
  207. !test_bit(AFS_VNODE_MOUNTPOINT, &dvnode->flags));
  208. i_size = i_size_read(&dvnode->netfs.inode);
  209. if (is_dir) {
  210. if (i_size < AFS_DIR_BLOCK_SIZE)
  211. return afs_bad(dvnode, afs_file_error_dir_small);
  212. if (i_size > AFS_DIR_BLOCK_SIZE * 1024) {
  213. trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);
  214. return -EFBIG;
  215. }
  216. } else {
  217. if (i_size > AFSPATHMAX) {
  218. trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);
  219. return -EFBIG;
  220. }
  221. }
  222. /* Expand the storage. TODO: Shrink the storage too. */
  223. if (dvnode->directory_size < i_size) {
  224. size_t cur_size = dvnode->directory_size;
  225. ret = netfs_alloc_folioq_buffer(NULL,
  226. &dvnode->directory, &cur_size, i_size,
  227. mapping_gfp_mask(dvnode->netfs.inode.i_mapping));
  228. dvnode->directory_size = cur_size;
  229. if (ret < 0)
  230. return ret;
  231. }
  232. iov_iter_folio_queue(&iter, ITER_DEST, dvnode->directory, 0, 0, dvnode->directory_size);
  233. /* AFS requires us to perform the read of a directory synchronously as
  234. * a single unit to avoid issues with the directory contents being
  235. * changed between reads.
  236. */
  237. ret = netfs_read_single(&dvnode->netfs.inode, file, &iter);
  238. if (ret >= 0) {
  239. i_size = i_size_read(&dvnode->netfs.inode);
  240. if (i_size > ret) {
  241. /* The content has grown, so we need to expand the
  242. * buffer.
  243. */
  244. ret = -ESTALE;
  245. } else if (is_dir) {
  246. int ret2 = afs_dir_check(dvnode);
  247. if (ret2 < 0)
  248. ret = ret2;
  249. } else if (i_size < folioq_folio_size(dvnode->directory, 0)) {
  250. /* NUL-terminate a symlink. */
  251. char *symlink = kmap_local_folio(folioq_folio(dvnode->directory, 0), 0);
  252. symlink[i_size] = 0;
  253. kunmap_local(symlink);
  254. }
  255. }
  256. return ret;
  257. }
  258. ssize_t afs_read_single(struct afs_vnode *dvnode, struct file *file)
  259. {
  260. ssize_t ret;
  261. fscache_use_cookie(afs_vnode_cache(dvnode), false);
  262. ret = afs_do_read_single(dvnode, file);
  263. fscache_unuse_cookie(afs_vnode_cache(dvnode), NULL, NULL);
  264. return ret;
  265. }
  266. /*
  267. * Read the directory into a folio_queue buffer in one go, scrubbing the
  268. * previous contents. We return -ESTALE if the caller needs to call us again.
  269. */
  270. ssize_t afs_read_dir(struct afs_vnode *dvnode, struct file *file)
  271. __acquires(&dvnode->validate_lock)
  272. {
  273. ssize_t ret;
  274. loff_t i_size;
  275. i_size = i_size_read(&dvnode->netfs.inode);
  276. ret = -ERESTARTSYS;
  277. if (down_read_killable(&dvnode->validate_lock) < 0)
  278. goto error;
  279. /* We only need to reread the data if it became invalid - or if we
  280. * haven't read it yet.
  281. */
  282. if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
  283. test_bit(AFS_VNODE_DIR_READ, &dvnode->flags)) {
  284. ret = i_size;
  285. goto valid;
  286. }
  287. up_read(&dvnode->validate_lock);
  288. if (down_write_killable(&dvnode->validate_lock) < 0)
  289. goto error;
  290. if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  291. afs_invalidate_cache(dvnode, 0);
  292. if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) ||
  293. !test_bit(AFS_VNODE_DIR_READ, &dvnode->flags)) {
  294. trace_afs_reload_dir(dvnode);
  295. ret = afs_read_single(dvnode, file);
  296. if (ret < 0)
  297. goto error_unlock;
  298. // TODO: Trim excess pages
  299. set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
  300. set_bit(AFS_VNODE_DIR_READ, &dvnode->flags);
  301. } else {
  302. ret = i_size;
  303. }
  304. downgrade_write(&dvnode->validate_lock);
  305. valid:
  306. return ret;
  307. error_unlock:
  308. up_write(&dvnode->validate_lock);
  309. error:
  310. _leave(" = %zd", ret);
  311. return ret;
  312. }
  313. /*
  314. * deal with one block in an AFS directory
  315. */
  316. static int afs_dir_iterate_block(struct afs_vnode *dvnode,
  317. struct dir_context *ctx,
  318. union afs_xdr_dir_block *block)
  319. {
  320. union afs_xdr_dirent *dire;
  321. unsigned int blknum, base, hdr, pos, next, nr_slots;
  322. size_t nlen;
  323. int tmp;
  324. blknum = ctx->pos / AFS_DIR_BLOCK_SIZE;
  325. base = blknum * AFS_DIR_SLOTS_PER_BLOCK;
  326. hdr = (blknum == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
  327. pos = DIV_ROUND_UP(ctx->pos, AFS_DIR_DIRENT_SIZE) - base;
  328. _enter("%llx,%x", ctx->pos, blknum);
  329. /* walk through the block, an entry at a time */
  330. for (unsigned int slot = hdr; slot < AFS_DIR_SLOTS_PER_BLOCK; slot = next) {
  331. /* skip entries marked unused in the bitmap */
  332. if (!(block->hdr.bitmap[slot / 8] &
  333. (1 << (slot % 8)))) {
  334. _debug("ENT[%x]: Unused", base + slot);
  335. next = slot + 1;
  336. if (next >= pos)
  337. ctx->pos = (base + next) * sizeof(union afs_xdr_dirent);
  338. continue;
  339. }
  340. /* got a valid entry */
  341. dire = &block->dirents[slot];
  342. nlen = strnlen(dire->u.name,
  343. (unsigned long)(block + 1) - (unsigned long)dire->u.name - 1);
  344. if (nlen > AFSNAMEMAX - 1) {
  345. _debug("ENT[%x]: Name too long (len %zx)",
  346. base + slot, nlen);
  347. return afs_bad(dvnode, afs_file_error_dir_name_too_long);
  348. }
  349. _debug("ENT[%x]: %s %zx \"%s\"",
  350. base + slot, (slot < pos ? "skip" : "fill"),
  351. nlen, dire->u.name);
  352. nr_slots = afs_dir_calc_slots(nlen);
  353. next = slot + nr_slots;
  354. if (next > AFS_DIR_SLOTS_PER_BLOCK) {
  355. _debug("ENT[%x]: extends beyond end dir block (len %zx)",
  356. base + slot, nlen);
  357. return afs_bad(dvnode, afs_file_error_dir_over_end);
  358. }
  359. /* Check that the name-extension dirents are all allocated */
  360. for (tmp = 1; tmp < nr_slots; tmp++) {
  361. unsigned int xslot = slot + tmp;
  362. if (!(block->hdr.bitmap[xslot / 8] & (1 << (xslot % 8)))) {
  363. _debug("ENT[%x]: Unmarked extension (%x/%x)",
  364. base + slot, tmp, nr_slots);
  365. return afs_bad(dvnode, afs_file_error_dir_unmarked_ext);
  366. }
  367. }
  368. /* skip if starts before the current position */
  369. if (slot < pos) {
  370. if (next > pos)
  371. ctx->pos = (base + next) * sizeof(union afs_xdr_dirent);
  372. continue;
  373. }
  374. /* found the next entry */
  375. if (!dir_emit(ctx, dire->u.name, nlen,
  376. ntohl(dire->u.vnode),
  377. (ctx->actor == afs_lookup_filldir ||
  378. ctx->actor == afs_lookup_one_filldir)?
  379. ntohl(dire->u.unique) : DT_UNKNOWN)) {
  380. _leave(" = 0 [full]");
  381. return 0;
  382. }
  383. ctx->pos = (base + next) * sizeof(union afs_xdr_dirent);
  384. }
  385. _leave(" = 1 [more]");
  386. return 1;
  387. }
  388. struct afs_dir_iteration_ctx {
  389. struct dir_context *dir_ctx;
  390. int error;
  391. };
  392. /*
  393. * Iterate through a kmapped directory segment.
  394. */
  395. static size_t afs_dir_iterate_step(void *iter_base, size_t progress, size_t len,
  396. void *priv, void *priv2)
  397. {
  398. struct afs_dir_iteration_ctx *ctx = priv2;
  399. struct afs_vnode *dvnode = priv;
  400. int ret;
  401. if (WARN_ON_ONCE(progress % AFS_DIR_BLOCK_SIZE ||
  402. len % AFS_DIR_BLOCK_SIZE)) {
  403. pr_err("Mis-iteration prog=%zx len=%zx\n",
  404. progress % AFS_DIR_BLOCK_SIZE,
  405. len % AFS_DIR_BLOCK_SIZE);
  406. return len;
  407. }
  408. do {
  409. ret = afs_dir_iterate_block(dvnode, ctx->dir_ctx, iter_base);
  410. if (ret != 1)
  411. break;
  412. ctx->dir_ctx->pos = round_up(ctx->dir_ctx->pos, AFS_DIR_BLOCK_SIZE);
  413. iter_base += AFS_DIR_BLOCK_SIZE;
  414. len -= AFS_DIR_BLOCK_SIZE;
  415. } while (len > 0);
  416. return len;
  417. }
  418. /*
  419. * Iterate through the directory folios.
  420. */
  421. static int afs_dir_iterate_contents(struct inode *dir, struct dir_context *dir_ctx)
  422. {
  423. struct afs_dir_iteration_ctx ctx = { .dir_ctx = dir_ctx };
  424. struct afs_vnode *dvnode = AFS_FS_I(dir);
  425. struct iov_iter iter;
  426. unsigned long long i_size = i_size_read(dir);
  427. /* Round the file position up to the next entry boundary */
  428. dir_ctx->pos = round_up(dir_ctx->pos, sizeof(union afs_xdr_dirent));
  429. if (i_size <= 0 || dir_ctx->pos >= i_size)
  430. return 0;
  431. iov_iter_folio_queue(&iter, ITER_SOURCE, dvnode->directory, 0, 0, i_size);
  432. iov_iter_advance(&iter, round_down(dir_ctx->pos, AFS_DIR_BLOCK_SIZE));
  433. iterate_folioq(&iter, iov_iter_count(&iter), dvnode, &ctx,
  434. afs_dir_iterate_step);
  435. if (ctx.error == -ESTALE)
  436. afs_invalidate_dir(dvnode, afs_dir_invalid_iter_stale);
  437. return ctx.error;
  438. }
  439. /*
  440. * iterate through the data blob that lists the contents of an AFS directory
  441. */
  442. static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
  443. struct file *file, afs_dataversion_t *_dir_version)
  444. {
  445. struct afs_vnode *dvnode = AFS_FS_I(dir);
  446. int retry_limit = 100;
  447. int ret;
  448. _enter("{%lu},%llx,,", dir->i_ino, ctx->pos);
  449. do {
  450. if (--retry_limit < 0) {
  451. pr_warn("afs_read_dir(): Too many retries\n");
  452. ret = -ESTALE;
  453. break;
  454. }
  455. ret = afs_read_dir(dvnode, file);
  456. if (ret < 0) {
  457. if (ret != -ESTALE)
  458. break;
  459. if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
  460. ret = -ESTALE;
  461. break;
  462. }
  463. continue;
  464. }
  465. *_dir_version = inode_peek_iversion_raw(dir);
  466. ret = afs_dir_iterate_contents(dir, ctx);
  467. up_read(&dvnode->validate_lock);
  468. } while (ret == -ESTALE);
  469. _leave(" = %d", ret);
  470. return ret;
  471. }
  472. /*
  473. * read an AFS directory
  474. */
  475. static int afs_readdir(struct file *file, struct dir_context *ctx)
  476. {
  477. afs_dataversion_t dir_version;
  478. return afs_dir_iterate(file_inode(file), ctx, file, &dir_version);
  479. }
  480. /*
  481. * Search the directory for a single name
  482. * - if afs_dir_iterate_block() spots this function, it'll pass the FID
  483. * uniquifier through dtype
  484. */
  485. static bool afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
  486. int nlen, loff_t fpos, u64 ino, unsigned dtype)
  487. {
  488. struct afs_lookup_one_cookie *cookie =
  489. container_of(ctx, struct afs_lookup_one_cookie, ctx);
  490. _enter("{%s,%u},%s,%u,,%llu,%u",
  491. cookie->name.name, cookie->name.len, name, nlen,
  492. (unsigned long long) ino, dtype);
  493. /* insanity checks first */
  494. BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
  495. BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
  496. if (cookie->name.len != nlen ||
  497. memcmp(cookie->name.name, name, nlen) != 0) {
  498. _leave(" = true [keep looking]");
  499. return true;
  500. }
  501. cookie->fid.vnode = ino;
  502. cookie->fid.unique = dtype;
  503. cookie->found = 1;
  504. _leave(" = false [found]");
  505. return false;
  506. }
  507. /*
  508. * Do a lookup of a single name in a directory
  509. * - just returns the FID the dentry name maps to if found
  510. */
  511. static int afs_do_lookup_one(struct inode *dir, const struct qstr *name,
  512. struct afs_fid *fid,
  513. afs_dataversion_t *_dir_version)
  514. {
  515. struct afs_super_info *as = dir->i_sb->s_fs_info;
  516. struct afs_lookup_one_cookie cookie = {
  517. .ctx.actor = afs_lookup_one_filldir,
  518. .name = *name,
  519. .fid.vid = as->volume->vid
  520. };
  521. int ret;
  522. _enter("{%lu},{%.*s},", dir->i_ino, name->len, name->name);
  523. /* search the directory */
  524. ret = afs_dir_iterate(dir, &cookie.ctx, NULL, _dir_version);
  525. if (ret < 0) {
  526. _leave(" = %d [iter]", ret);
  527. return ret;
  528. }
  529. if (!cookie.found) {
  530. _leave(" = -ENOENT [not found]");
  531. return -ENOENT;
  532. }
  533. *fid = cookie.fid;
  534. _leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique);
  535. return 0;
  536. }
  537. /*
  538. * search the directory for a name
  539. * - if afs_dir_iterate_block() spots this function, it'll pass the FID
  540. * uniquifier through dtype
  541. */
  542. static bool afs_lookup_filldir(struct dir_context *ctx, const char *name,
  543. int nlen, loff_t fpos, u64 ino, unsigned dtype)
  544. {
  545. struct afs_lookup_cookie *cookie =
  546. container_of(ctx, struct afs_lookup_cookie, ctx);
  547. _enter("{%s,%u},%s,%u,,%llu,%u",
  548. cookie->name.name, cookie->name.len, name, nlen,
  549. (unsigned long long) ino, dtype);
  550. /* insanity checks first */
  551. BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
  552. BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
  553. if (cookie->nr_fids < 50) {
  554. cookie->fids[cookie->nr_fids].vnode = ino;
  555. cookie->fids[cookie->nr_fids].unique = dtype;
  556. cookie->nr_fids++;
  557. }
  558. return cookie->nr_fids < 50;
  559. }
  560. /*
  561. * Deal with the result of a successful lookup operation. Turn all the files
  562. * into inodes and save the first one - which is the one we actually want.
  563. */
  564. static void afs_do_lookup_success(struct afs_operation *op)
  565. {
  566. struct afs_vnode_param *vp;
  567. struct afs_vnode *vnode;
  568. struct inode *inode;
  569. u32 abort_code;
  570. int i;
  571. _enter("");
  572. for (i = 0; i < op->nr_files; i++) {
  573. switch (i) {
  574. case 0:
  575. vp = &op->file[0];
  576. abort_code = vp->scb.status.abort_code;
  577. if (abort_code != 0) {
  578. op->call_abort_code = abort_code;
  579. afs_op_set_error(op, afs_abort_to_error(abort_code));
  580. op->cumul_error.abort_code = abort_code;
  581. }
  582. break;
  583. case 1:
  584. vp = &op->file[1];
  585. break;
  586. default:
  587. vp = &op->more_files[i - 2];
  588. break;
  589. }
  590. if (vp->scb.status.abort_code)
  591. trace_afs_bulkstat_error(op, &vp->fid, i, vp->scb.status.abort_code);
  592. if (!vp->scb.have_status && !vp->scb.have_error)
  593. continue;
  594. _debug("do [%u]", i);
  595. if (vp->vnode) {
  596. if (!test_bit(AFS_VNODE_UNSET, &vp->vnode->flags))
  597. afs_vnode_commit_status(op, vp);
  598. } else if (vp->scb.status.abort_code == 0) {
  599. inode = afs_iget(op, vp);
  600. if (!IS_ERR(inode)) {
  601. vnode = AFS_FS_I(inode);
  602. afs_cache_permit(vnode, op->key,
  603. 0 /* Assume vnode->cb_break is 0 */ +
  604. op->cb_v_break,
  605. &vp->scb);
  606. vp->vnode = vnode;
  607. vp->put_vnode = true;
  608. }
  609. } else {
  610. _debug("- abort %d %llx:%llx.%x",
  611. vp->scb.status.abort_code,
  612. vp->fid.vid, vp->fid.vnode, vp->fid.unique);
  613. }
  614. }
  615. _leave("");
  616. }
  617. static const struct afs_operation_ops afs_inline_bulk_status_operation = {
  618. .issue_afs_rpc = afs_fs_inline_bulk_status,
  619. .issue_yfs_rpc = yfs_fs_inline_bulk_status,
  620. .success = afs_do_lookup_success,
  621. };
  622. static const struct afs_operation_ops afs_lookup_fetch_status_operation = {
  623. .issue_afs_rpc = afs_fs_fetch_status,
  624. .issue_yfs_rpc = yfs_fs_fetch_status,
  625. .success = afs_do_lookup_success,
  626. .aborted = afs_check_for_remote_deletion,
  627. };
  628. /*
  629. * See if we know that the server we expect to use doesn't support
  630. * FS.InlineBulkStatus.
  631. */
  632. static bool afs_server_supports_ibulk(struct afs_vnode *dvnode)
  633. {
  634. struct afs_server_list *slist;
  635. struct afs_volume *volume = dvnode->volume;
  636. struct afs_server *server;
  637. bool ret = true;
  638. int i;
  639. if (!test_bit(AFS_VOLUME_MAYBE_NO_IBULK, &volume->flags))
  640. return true;
  641. rcu_read_lock();
  642. slist = rcu_dereference(volume->servers);
  643. for (i = 0; i < slist->nr_servers; i++) {
  644. server = slist->servers[i].server;
  645. if (server == dvnode->cb_server) {
  646. if (test_bit(AFS_SERVER_FL_NO_IBULK, &server->flags))
  647. ret = false;
  648. break;
  649. }
  650. }
  651. rcu_read_unlock();
  652. return ret;
  653. }
  654. /*
  655. * Do a lookup in a directory. We make use of bulk lookup to query a slew of
  656. * files in one go and create inodes for them. The inode of the file we were
  657. * asked for is returned.
  658. */
  659. static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry)
  660. {
  661. struct afs_lookup_cookie *cookie;
  662. struct afs_vnode_param *vp;
  663. struct afs_operation *op;
  664. struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode;
  665. struct inode *inode = NULL, *ti;
  666. afs_dataversion_t data_version = READ_ONCE(dvnode->status.data_version);
  667. bool supports_ibulk, isnew;
  668. long ret;
  669. int i;
  670. _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
  671. cookie = kzalloc_obj(struct afs_lookup_cookie);
  672. if (!cookie)
  673. return ERR_PTR(-ENOMEM);
  674. for (i = 0; i < ARRAY_SIZE(cookie->fids); i++)
  675. cookie->fids[i].vid = dvnode->fid.vid;
  676. cookie->ctx.actor = afs_lookup_filldir;
  677. cookie->name = dentry->d_name;
  678. cookie->nr_fids = 2; /* slot 1 is saved for the fid we actually want
  679. * and slot 0 for the directory */
  680. /* Search the directory for the named entry using the hash table... */
  681. ret = afs_dir_search(dvnode, &dentry->d_name, &cookie->fids[1], &data_version);
  682. if (ret < 0)
  683. goto out;
  684. supports_ibulk = afs_server_supports_ibulk(dvnode);
  685. if (supports_ibulk) {
  686. /* ...then scan linearly from that point for entries to lookup-ahead. */
  687. cookie->ctx.pos = (ret + 1) * AFS_DIR_DIRENT_SIZE;
  688. afs_dir_iterate(dir, &cookie->ctx, NULL, &data_version);
  689. }
  690. dentry->d_fsdata = (void *)(unsigned long)data_version;
  691. /* Check to see if we already have an inode for the primary fid. */
  692. inode = ilookup5(dir->i_sb, cookie->fids[1].vnode,
  693. afs_ilookup5_test_by_fid, &cookie->fids[1]);
  694. if (inode)
  695. goto out; /* We do */
  696. /* Okay, we didn't find it. We need to query the server - and whilst
  697. * we're doing that, we're going to attempt to look up a bunch of other
  698. * vnodes also.
  699. */
  700. op = afs_alloc_operation(NULL, dvnode->volume);
  701. if (IS_ERR(op)) {
  702. ret = PTR_ERR(op);
  703. goto out;
  704. }
  705. afs_op_set_vnode(op, 0, dvnode);
  706. afs_op_set_fid(op, 1, &cookie->fids[1]);
  707. op->nr_files = cookie->nr_fids;
  708. _debug("nr_files %u", op->nr_files);
  709. /* Need space for examining all the selected files */
  710. if (op->nr_files > 2) {
  711. op->more_files = kvzalloc_objs(struct afs_vnode_param,
  712. op->nr_files - 2);
  713. if (!op->more_files) {
  714. afs_op_nomem(op);
  715. goto out_op;
  716. }
  717. for (i = 2; i < op->nr_files; i++) {
  718. vp = &op->more_files[i - 2];
  719. vp->fid = cookie->fids[i];
  720. /* Find any inodes that already exist and get their
  721. * callback counters.
  722. */
  723. ti = ilookup5_nowait(dir->i_sb, vp->fid.vnode,
  724. afs_ilookup5_test_by_fid, &vp->fid, &isnew);
  725. if (!IS_ERR_OR_NULL(ti)) {
  726. vnode = AFS_FS_I(ti);
  727. vp->dv_before = vnode->status.data_version;
  728. vp->cb_break_before = afs_calc_vnode_cb_break(vnode);
  729. vp->vnode = vnode;
  730. vp->put_vnode = true;
  731. vp->speculative = true; /* vnode not locked */
  732. }
  733. }
  734. }
  735. /* Try FS.InlineBulkStatus first. Abort codes for the individual
  736. * lookups contained therein are stored in the reply without aborting
  737. * the whole operation.
  738. */
  739. afs_op_set_error(op, -ENOTSUPP);
  740. if (supports_ibulk) {
  741. op->ops = &afs_inline_bulk_status_operation;
  742. afs_begin_vnode_operation(op);
  743. afs_wait_for_operation(op);
  744. }
  745. if (afs_op_error(op) == -ENOTSUPP) {
  746. /* We could try FS.BulkStatus next, but this aborts the entire
  747. * op if any of the lookups fails - so, for the moment, revert
  748. * to FS.FetchStatus for op->file[1].
  749. */
  750. op->fetch_status.which = 1;
  751. op->ops = &afs_lookup_fetch_status_operation;
  752. afs_begin_vnode_operation(op);
  753. afs_wait_for_operation(op);
  754. }
  755. out_op:
  756. if (!afs_op_error(op)) {
  757. if (op->file[1].scb.status.abort_code) {
  758. afs_op_accumulate_error(op, -ECONNABORTED,
  759. op->file[1].scb.status.abort_code);
  760. } else {
  761. inode = &op->file[1].vnode->netfs.inode;
  762. op->file[1].vnode = NULL;
  763. }
  764. }
  765. if (op->file[0].scb.have_status)
  766. dentry->d_fsdata = (void *)(unsigned long)op->file[0].scb.status.data_version;
  767. else
  768. dentry->d_fsdata = (void *)(unsigned long)op->file[0].dv_before;
  769. ret = afs_put_operation(op);
  770. out:
  771. kfree(cookie);
  772. _leave("");
  773. return inode ?: ERR_PTR(ret);
  774. }
  775. /*
  776. * Look up an entry in a directory with @sys substitution.
  777. */
  778. static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry)
  779. {
  780. struct afs_sysnames *subs;
  781. struct afs_net *net = afs_i2net(dir);
  782. struct dentry *ret;
  783. char *buf, *p, *name;
  784. int len, i;
  785. _enter("");
  786. ret = ERR_PTR(-ENOMEM);
  787. p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
  788. if (!buf)
  789. goto out_p;
  790. if (dentry->d_name.len > 4) {
  791. memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
  792. p += dentry->d_name.len - 4;
  793. }
  794. /* There is an ordered list of substitutes that we have to try. */
  795. read_lock(&net->sysnames_lock);
  796. subs = net->sysnames;
  797. refcount_inc(&subs->usage);
  798. read_unlock(&net->sysnames_lock);
  799. for (i = 0; i < subs->nr; i++) {
  800. name = subs->subs[i];
  801. len = dentry->d_name.len - 4 + strlen(name);
  802. if (len >= AFSNAMEMAX) {
  803. ret = ERR_PTR(-ENAMETOOLONG);
  804. goto out_s;
  805. }
  806. strcpy(p, name);
  807. ret = lookup_noperm(&QSTR(buf), dentry->d_parent);
  808. if (IS_ERR(ret) || d_is_positive(ret))
  809. goto out_s;
  810. dput(ret);
  811. }
  812. /* We don't want to d_add() the @sys dentry here as we don't want to
  813. * the cached dentry to hide changes to the sysnames list.
  814. */
  815. ret = NULL;
  816. out_s:
  817. afs_put_sysnames(subs);
  818. kfree(buf);
  819. out_p:
  820. return ret;
  821. }
  822. /*
  823. * look up an entry in a directory
  824. */
  825. static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
  826. unsigned int flags)
  827. {
  828. struct afs_vnode *dvnode = AFS_FS_I(dir);
  829. struct afs_fid fid = {};
  830. struct inode *inode;
  831. struct dentry *d;
  832. int ret;
  833. _enter("{%llx:%llu},%p{%pd},",
  834. dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
  835. ASSERTCMP(d_inode(dentry), ==, NULL);
  836. if (dentry->d_name.len >= AFSNAMEMAX) {
  837. _leave(" = -ENAMETOOLONG");
  838. return ERR_PTR(-ENAMETOOLONG);
  839. }
  840. if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
  841. _leave(" = -ESTALE");
  842. return ERR_PTR(-ESTALE);
  843. }
  844. ret = afs_validate(dvnode, NULL);
  845. if (ret < 0) {
  846. afs_dir_unuse_cookie(dvnode, ret);
  847. _leave(" = %d [val]", ret);
  848. return ERR_PTR(ret);
  849. }
  850. if (dentry->d_name.len >= 4 &&
  851. dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
  852. dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
  853. dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
  854. dentry->d_name.name[dentry->d_name.len - 1] == 's')
  855. return afs_lookup_atsys(dir, dentry);
  856. afs_stat_v(dvnode, n_lookup);
  857. inode = afs_do_lookup(dir, dentry);
  858. if (inode == ERR_PTR(-ENOENT))
  859. inode = NULL;
  860. else if (!IS_ERR_OR_NULL(inode))
  861. fid = AFS_FS_I(inode)->fid;
  862. _debug("splice %p", dentry->d_inode);
  863. d = d_splice_alias(inode, dentry);
  864. if (!IS_ERR_OR_NULL(d)) {
  865. d->d_fsdata = dentry->d_fsdata;
  866. trace_afs_lookup(dvnode, &d->d_name, &fid);
  867. } else {
  868. trace_afs_lookup(dvnode, &dentry->d_name, &fid);
  869. }
  870. _leave("");
  871. return d;
  872. }
  873. /*
  874. * Check the validity of a dentry under RCU conditions.
  875. */
  876. static int afs_d_revalidate_rcu(struct afs_vnode *dvnode, struct dentry *dentry)
  877. {
  878. long dir_version, de_version;
  879. _enter("%p", dentry);
  880. if (test_bit(AFS_VNODE_DELETED, &dvnode->flags))
  881. return -ECHILD;
  882. if (!afs_check_validity(dvnode))
  883. return -ECHILD;
  884. /* We only need to invalidate a dentry if the server's copy changed
  885. * behind our back. If we made the change, it's no problem. Note that
  886. * on a 32-bit system, we only have 32 bits in the dentry to store the
  887. * version.
  888. */
  889. dir_version = (long)READ_ONCE(dvnode->status.data_version);
  890. de_version = (long)READ_ONCE(dentry->d_fsdata);
  891. if (de_version != dir_version) {
  892. dir_version = (long)READ_ONCE(dvnode->invalid_before);
  893. if (de_version - dir_version < 0)
  894. return -ECHILD;
  895. }
  896. return 1; /* Still valid */
  897. }
  898. /*
  899. * check that a dentry lookup hit has found a valid entry
  900. * - NOTE! the hit can be a negative hit too, so we can't assume we have an
  901. * inode
  902. */
  903. static int afs_d_revalidate(struct inode *parent_dir, const struct qstr *name,
  904. struct dentry *dentry, unsigned int flags)
  905. {
  906. struct afs_vnode *vnode, *dir = AFS_FS_I(parent_dir);
  907. struct afs_fid fid;
  908. struct inode *inode;
  909. struct key *key;
  910. afs_dataversion_t dir_version, invalid_before;
  911. long de_version;
  912. int ret;
  913. if (flags & LOOKUP_RCU)
  914. return afs_d_revalidate_rcu(dir, dentry);
  915. if (d_really_is_positive(dentry)) {
  916. vnode = AFS_FS_I(d_inode(dentry));
  917. _enter("{v={%llx:%llu} n=%pd fl=%lx},",
  918. vnode->fid.vid, vnode->fid.vnode, dentry,
  919. vnode->flags);
  920. } else {
  921. _enter("{neg n=%pd}", dentry);
  922. }
  923. key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
  924. if (IS_ERR(key))
  925. key = NULL;
  926. /* validate the parent directory */
  927. ret = afs_validate(dir, key);
  928. if (ret == -ERESTARTSYS) {
  929. key_put(key);
  930. return ret;
  931. }
  932. if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
  933. _debug("%pd: parent dir deleted", dentry);
  934. goto not_found;
  935. }
  936. /* We only need to invalidate a dentry if the server's copy changed
  937. * behind our back. If we made the change, it's no problem. Note that
  938. * on a 32-bit system, we only have 32 bits in the dentry to store the
  939. * version.
  940. */
  941. dir_version = dir->status.data_version;
  942. de_version = (long)dentry->d_fsdata;
  943. if (de_version == (long)dir_version)
  944. goto out_valid_noupdate;
  945. invalid_before = dir->invalid_before;
  946. if (de_version - (long)invalid_before >= 0)
  947. goto out_valid;
  948. _debug("dir modified");
  949. afs_stat_v(dir, n_reval);
  950. /* search the directory for this vnode */
  951. ret = afs_do_lookup_one(&dir->netfs.inode, name, &fid, &dir_version);
  952. switch (ret) {
  953. case 0:
  954. /* the filename maps to something */
  955. if (d_really_is_negative(dentry))
  956. goto not_found;
  957. inode = d_inode(dentry);
  958. if (is_bad_inode(inode)) {
  959. printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
  960. dentry);
  961. goto not_found;
  962. }
  963. vnode = AFS_FS_I(inode);
  964. /* if the vnode ID has changed, then the dirent points to a
  965. * different file */
  966. if (fid.vnode != vnode->fid.vnode) {
  967. _debug("%pd: dirent changed [%llu != %llu]",
  968. dentry, fid.vnode,
  969. vnode->fid.vnode);
  970. goto not_found;
  971. }
  972. /* if the vnode ID uniqifier has changed, then the file has
  973. * been deleted and replaced, and the original vnode ID has
  974. * been reused */
  975. if (fid.unique != vnode->fid.unique) {
  976. _debug("%pd: file deleted (uq %u -> %u I:%u)",
  977. dentry, fid.unique,
  978. vnode->fid.unique,
  979. vnode->netfs.inode.i_generation);
  980. goto not_found;
  981. }
  982. goto out_valid;
  983. case -ENOENT:
  984. /* the filename is unknown */
  985. _debug("%pd: dirent not found", dentry);
  986. if (d_really_is_positive(dentry))
  987. goto not_found;
  988. goto out_valid;
  989. default:
  990. _debug("failed to iterate parent %pd2: %d", dentry, ret);
  991. goto not_found;
  992. }
  993. out_valid:
  994. dentry->d_fsdata = (void *)(unsigned long)dir_version;
  995. out_valid_noupdate:
  996. key_put(key);
  997. _leave(" = 1 [valid]");
  998. return 1;
  999. not_found:
  1000. _debug("dropping dentry %pd2", dentry);
  1001. key_put(key);
  1002. _leave(" = 0 [bad]");
  1003. return 0;
  1004. }
  1005. /*
  1006. * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
  1007. * sleep)
  1008. * - called from dput() when d_count is going to 0.
  1009. * - return 1 to request dentry be unhashed, 0 otherwise
  1010. */
  1011. static int afs_d_delete(const struct dentry *dentry)
  1012. {
  1013. _enter("%pd", dentry);
  1014. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1015. goto zap;
  1016. if (d_really_is_positive(dentry) &&
  1017. (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) ||
  1018. test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
  1019. goto zap;
  1020. _leave(" = 0 [keep]");
  1021. return 0;
  1022. zap:
  1023. _leave(" = 1 [zap]");
  1024. return 1;
  1025. }
  1026. /*
  1027. * Clean up sillyrename files on dentry removal.
  1028. */
  1029. static void afs_d_iput(struct dentry *dentry, struct inode *inode)
  1030. {
  1031. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1032. afs_silly_iput(dentry, inode);
  1033. iput(inode);
  1034. }
  1035. /*
  1036. * handle dentry release
  1037. */
  1038. void afs_d_release(struct dentry *dentry)
  1039. {
  1040. _enter("%pd", dentry);
  1041. }
  1042. void afs_check_for_remote_deletion(struct afs_operation *op)
  1043. {
  1044. struct afs_vnode *vnode = op->file[0].vnode;
  1045. switch (afs_op_abort_code(op)) {
  1046. case VNOVNODE:
  1047. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  1048. clear_nlink(&vnode->netfs.inode);
  1049. afs_break_callback(vnode, afs_cb_break_for_deleted);
  1050. }
  1051. }
  1052. /*
  1053. * Create a new inode for create/mkdir/symlink
  1054. */
  1055. static void afs_vnode_new_inode(struct afs_operation *op)
  1056. {
  1057. struct afs_vnode_param *dvp = &op->file[0];
  1058. struct afs_vnode_param *vp = &op->file[1];
  1059. struct afs_vnode *vnode;
  1060. struct inode *inode;
  1061. _enter("");
  1062. ASSERTCMP(afs_op_error(op), ==, 0);
  1063. inode = afs_iget(op, vp);
  1064. if (IS_ERR(inode)) {
  1065. /* ENOMEM or EINTR at a really inconvenient time - just abandon
  1066. * the new directory on the server.
  1067. */
  1068. afs_op_accumulate_error(op, PTR_ERR(inode), 0);
  1069. return;
  1070. }
  1071. vnode = AFS_FS_I(inode);
  1072. set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
  1073. if (S_ISDIR(inode->i_mode))
  1074. afs_mkdir_init_dir(vnode, dvp->vnode);
  1075. else if (S_ISLNK(inode->i_mode))
  1076. afs_init_new_symlink(vnode, op);
  1077. if (!afs_op_error(op))
  1078. afs_cache_permit(vnode, op->key, vnode->cb_break, &vp->scb);
  1079. d_instantiate(op->dentry, inode);
  1080. }
  1081. static void afs_create_success(struct afs_operation *op)
  1082. {
  1083. _enter("op=%08x", op->debug_id);
  1084. op->ctime = op->file[0].scb.status.mtime_client;
  1085. afs_vnode_commit_status(op, &op->file[0]);
  1086. afs_update_dentry_version(op, &op->file[0], op->dentry);
  1087. afs_vnode_new_inode(op);
  1088. }
  1089. static void afs_create_edit_dir(struct afs_operation *op)
  1090. {
  1091. struct netfs_cache_resources cres = {};
  1092. struct afs_vnode_param *dvp = &op->file[0];
  1093. struct afs_vnode_param *vp = &op->file[1];
  1094. struct afs_vnode *dvnode = dvp->vnode;
  1095. _enter("op=%08x", op->debug_id);
  1096. fscache_begin_write_operation(&cres, afs_vnode_cache(dvnode));
  1097. down_write(&dvnode->validate_lock);
  1098. if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
  1099. dvnode->status.data_version == dvp->dv_before + dvp->dv_delta)
  1100. afs_edit_dir_add(dvnode, &op->dentry->d_name, &vp->fid,
  1101. op->create.reason);
  1102. up_write(&dvnode->validate_lock);
  1103. fscache_end_operation(&cres);
  1104. }
  1105. static void afs_create_put(struct afs_operation *op)
  1106. {
  1107. _enter("op=%08x", op->debug_id);
  1108. if (afs_op_error(op))
  1109. d_drop(op->dentry);
  1110. }
  1111. static const struct afs_operation_ops afs_mkdir_operation = {
  1112. .issue_afs_rpc = afs_fs_make_dir,
  1113. .issue_yfs_rpc = yfs_fs_make_dir,
  1114. .success = afs_create_success,
  1115. .aborted = afs_check_for_remote_deletion,
  1116. .edit_dir = afs_create_edit_dir,
  1117. .put = afs_create_put,
  1118. };
  1119. /*
  1120. * create a directory on an AFS filesystem
  1121. */
  1122. static struct dentry *afs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
  1123. struct dentry *dentry, umode_t mode)
  1124. {
  1125. struct afs_operation *op;
  1126. struct afs_vnode *dvnode = AFS_FS_I(dir);
  1127. int ret;
  1128. _enter("{%llx:%llu},{%pd},%ho",
  1129. dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
  1130. op = afs_alloc_operation(NULL, dvnode->volume);
  1131. if (IS_ERR(op)) {
  1132. d_drop(dentry);
  1133. return ERR_CAST(op);
  1134. }
  1135. fscache_use_cookie(afs_vnode_cache(dvnode), true);
  1136. afs_op_set_vnode(op, 0, dvnode);
  1137. op->file[0].dv_delta = 1;
  1138. op->file[0].modification = true;
  1139. op->file[0].update_ctime = true;
  1140. op->dentry = dentry;
  1141. op->create.mode = S_IFDIR | mode;
  1142. op->create.reason = afs_edit_dir_for_mkdir;
  1143. op->mtime = current_time(dir);
  1144. op->ops = &afs_mkdir_operation;
  1145. ret = afs_do_sync_operation(op);
  1146. afs_dir_unuse_cookie(dvnode, ret);
  1147. return ERR_PTR(ret);
  1148. }
  1149. /*
  1150. * Remove a subdir from a directory.
  1151. */
  1152. static void afs_dir_remove_subdir(struct dentry *dentry)
  1153. {
  1154. if (d_really_is_positive(dentry)) {
  1155. struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
  1156. clear_nlink(&vnode->netfs.inode);
  1157. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  1158. afs_clear_cb_promise(vnode, afs_cb_promise_clear_rmdir);
  1159. afs_invalidate_dir(vnode, afs_dir_invalid_subdir_removed);
  1160. }
  1161. }
  1162. static void afs_rmdir_success(struct afs_operation *op)
  1163. {
  1164. _enter("op=%08x", op->debug_id);
  1165. op->ctime = op->file[0].scb.status.mtime_client;
  1166. afs_vnode_commit_status(op, &op->file[0]);
  1167. afs_update_dentry_version(op, &op->file[0], op->dentry);
  1168. }
  1169. static void afs_rmdir_edit_dir(struct afs_operation *op)
  1170. {
  1171. struct netfs_cache_resources cres = {};
  1172. struct afs_vnode_param *dvp = &op->file[0];
  1173. struct afs_vnode *dvnode = dvp->vnode;
  1174. _enter("op=%08x", op->debug_id);
  1175. afs_dir_remove_subdir(op->dentry);
  1176. fscache_begin_write_operation(&cres, afs_vnode_cache(dvnode));
  1177. down_write(&dvnode->validate_lock);
  1178. if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
  1179. dvnode->status.data_version == dvp->dv_before + dvp->dv_delta)
  1180. afs_edit_dir_remove(dvnode, &op->dentry->d_name,
  1181. afs_edit_dir_for_rmdir);
  1182. up_write(&dvnode->validate_lock);
  1183. fscache_end_operation(&cres);
  1184. }
  1185. static void afs_rmdir_put(struct afs_operation *op)
  1186. {
  1187. _enter("op=%08x", op->debug_id);
  1188. if (op->file[1].vnode)
  1189. up_write(&op->file[1].vnode->rmdir_lock);
  1190. }
  1191. static const struct afs_operation_ops afs_rmdir_operation = {
  1192. .issue_afs_rpc = afs_fs_remove_dir,
  1193. .issue_yfs_rpc = yfs_fs_remove_dir,
  1194. .success = afs_rmdir_success,
  1195. .aborted = afs_check_for_remote_deletion,
  1196. .edit_dir = afs_rmdir_edit_dir,
  1197. .put = afs_rmdir_put,
  1198. };
  1199. /*
  1200. * remove a directory from an AFS filesystem
  1201. */
  1202. static int afs_rmdir(struct inode *dir, struct dentry *dentry)
  1203. {
  1204. struct afs_operation *op;
  1205. struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
  1206. int ret;
  1207. _enter("{%llx:%llu},{%pd}",
  1208. dvnode->fid.vid, dvnode->fid.vnode, dentry);
  1209. op = afs_alloc_operation(NULL, dvnode->volume);
  1210. if (IS_ERR(op))
  1211. return PTR_ERR(op);
  1212. fscache_use_cookie(afs_vnode_cache(dvnode), true);
  1213. afs_op_set_vnode(op, 0, dvnode);
  1214. op->file[0].dv_delta = 1;
  1215. op->file[0].modification = true;
  1216. op->file[0].update_ctime = true;
  1217. op->dentry = dentry;
  1218. op->ops = &afs_rmdir_operation;
  1219. /* Try to make sure we have a callback promise on the victim. */
  1220. if (d_really_is_positive(dentry)) {
  1221. vnode = AFS_FS_I(d_inode(dentry));
  1222. ret = afs_validate(vnode, op->key);
  1223. if (ret < 0)
  1224. goto error;
  1225. }
  1226. if (vnode) {
  1227. ret = down_write_killable(&vnode->rmdir_lock);
  1228. if (ret < 0)
  1229. goto error;
  1230. op->file[1].vnode = vnode;
  1231. }
  1232. ret = afs_do_sync_operation(op);
  1233. /* Not all systems that can host afs servers have ENOTEMPTY. */
  1234. if (ret == -EEXIST)
  1235. ret = -ENOTEMPTY;
  1236. out:
  1237. afs_dir_unuse_cookie(dvnode, ret);
  1238. return ret;
  1239. error:
  1240. ret = afs_put_operation(op);
  1241. goto out;
  1242. }
  1243. /*
  1244. * Remove a link to a file or symlink from a directory.
  1245. *
  1246. * If the file was not deleted due to excess hard links, the fileserver will
  1247. * break the callback promise on the file - if it had one - before it returns
  1248. * to us, and if it was deleted, it won't
  1249. *
  1250. * However, if we didn't have a callback promise outstanding, or it was
  1251. * outstanding on a different server, then it won't break it either...
  1252. */
  1253. static void afs_dir_remove_link(struct afs_operation *op)
  1254. {
  1255. struct afs_vnode *dvnode = op->file[0].vnode;
  1256. struct afs_vnode *vnode = op->file[1].vnode;
  1257. struct dentry *dentry = op->dentry;
  1258. int ret;
  1259. if (afs_op_error(op) ||
  1260. (op->file[1].scb.have_status && op->file[1].scb.have_error))
  1261. return;
  1262. if (d_really_is_positive(dentry))
  1263. return;
  1264. if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
  1265. /* Already done */
  1266. } else if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
  1267. write_seqlock(&vnode->cb_lock);
  1268. drop_nlink(&vnode->netfs.inode);
  1269. if (vnode->netfs.inode.i_nlink == 0) {
  1270. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  1271. __afs_break_callback(vnode, afs_cb_break_for_unlink);
  1272. }
  1273. write_sequnlock(&vnode->cb_lock);
  1274. } else {
  1275. afs_break_callback(vnode, afs_cb_break_for_unlink);
  1276. if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
  1277. _debug("AFS_VNODE_DELETED");
  1278. ret = afs_validate(vnode, op->key);
  1279. if (ret != -ESTALE)
  1280. afs_op_set_error(op, ret);
  1281. }
  1282. _debug("nlink %d [val %d]", vnode->netfs.inode.i_nlink, afs_op_error(op));
  1283. }
  1284. static void afs_unlink_success(struct afs_operation *op)
  1285. {
  1286. _enter("op=%08x", op->debug_id);
  1287. op->ctime = op->file[0].scb.status.mtime_client;
  1288. afs_check_dir_conflict(op, &op->file[0]);
  1289. afs_vnode_commit_status(op, &op->file[0]);
  1290. afs_vnode_commit_status(op, &op->file[1]);
  1291. afs_update_dentry_version(op, &op->file[0], op->dentry);
  1292. afs_dir_remove_link(op);
  1293. }
  1294. static void afs_unlink_edit_dir(struct afs_operation *op)
  1295. {
  1296. struct netfs_cache_resources cres = {};
  1297. struct afs_vnode_param *dvp = &op->file[0];
  1298. struct afs_vnode *dvnode = dvp->vnode;
  1299. _enter("op=%08x", op->debug_id);
  1300. fscache_begin_write_operation(&cres, afs_vnode_cache(dvnode));
  1301. down_write(&dvnode->validate_lock);
  1302. if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
  1303. dvnode->status.data_version == dvp->dv_before + dvp->dv_delta)
  1304. afs_edit_dir_remove(dvnode, &op->dentry->d_name,
  1305. afs_edit_dir_for_unlink);
  1306. up_write(&dvnode->validate_lock);
  1307. fscache_end_operation(&cres);
  1308. }
  1309. static void afs_unlink_put(struct afs_operation *op)
  1310. {
  1311. _enter("op=%08x", op->debug_id);
  1312. if (op->unlink.need_rehash && afs_op_error(op) < 0 && afs_op_error(op) != -ENOENT)
  1313. d_rehash(op->dentry);
  1314. }
  1315. static const struct afs_operation_ops afs_unlink_operation = {
  1316. .issue_afs_rpc = afs_fs_remove_file,
  1317. .issue_yfs_rpc = yfs_fs_remove_file,
  1318. .success = afs_unlink_success,
  1319. .aborted = afs_check_for_remote_deletion,
  1320. .edit_dir = afs_unlink_edit_dir,
  1321. .put = afs_unlink_put,
  1322. };
  1323. /*
  1324. * Remove a file or symlink from an AFS filesystem.
  1325. */
  1326. static int afs_unlink(struct inode *dir, struct dentry *dentry)
  1327. {
  1328. struct afs_operation *op;
  1329. struct afs_vnode *dvnode = AFS_FS_I(dir);
  1330. struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
  1331. int ret;
  1332. _enter("{%llx:%llu},{%pd}",
  1333. dvnode->fid.vid, dvnode->fid.vnode, dentry);
  1334. if (dentry->d_name.len >= AFSNAMEMAX)
  1335. return -ENAMETOOLONG;
  1336. op = afs_alloc_operation(NULL, dvnode->volume);
  1337. if (IS_ERR(op))
  1338. return PTR_ERR(op);
  1339. fscache_use_cookie(afs_vnode_cache(dvnode), true);
  1340. afs_op_set_vnode(op, 0, dvnode);
  1341. op->file[0].dv_delta = 1;
  1342. op->file[0].modification = true;
  1343. op->file[0].update_ctime = true;
  1344. /* Try to make sure we have a callback promise on the victim. */
  1345. ret = afs_validate(vnode, op->key);
  1346. if (ret < 0) {
  1347. afs_op_set_error(op, ret);
  1348. goto error;
  1349. }
  1350. spin_lock(&dentry->d_lock);
  1351. if (d_count(dentry) > 1) {
  1352. spin_unlock(&dentry->d_lock);
  1353. /* Start asynchronous writeout of the inode */
  1354. write_inode_now(d_inode(dentry), 0);
  1355. afs_op_set_error(op, afs_sillyrename(dvnode, vnode, dentry, op->key));
  1356. goto error;
  1357. }
  1358. if (!d_unhashed(dentry)) {
  1359. /* Prevent a race with RCU lookup. */
  1360. __d_drop(dentry);
  1361. op->unlink.need_rehash = true;
  1362. }
  1363. spin_unlock(&dentry->d_lock);
  1364. op->file[1].vnode = vnode;
  1365. op->file[1].update_ctime = true;
  1366. op->file[1].op_unlinked = true;
  1367. op->dentry = dentry;
  1368. op->ops = &afs_unlink_operation;
  1369. afs_begin_vnode_operation(op);
  1370. afs_wait_for_operation(op);
  1371. /* If there was a conflict with a third party, check the status of the
  1372. * unlinked vnode.
  1373. */
  1374. if (afs_op_error(op) == 0 && (op->flags & AFS_OPERATION_DIR_CONFLICT)) {
  1375. op->file[1].update_ctime = false;
  1376. op->fetch_status.which = 1;
  1377. op->ops = &afs_fetch_status_operation;
  1378. afs_begin_vnode_operation(op);
  1379. afs_wait_for_operation(op);
  1380. }
  1381. error:
  1382. ret = afs_put_operation(op);
  1383. afs_dir_unuse_cookie(dvnode, ret);
  1384. return ret;
  1385. }
  1386. static const struct afs_operation_ops afs_create_operation = {
  1387. .issue_afs_rpc = afs_fs_create_file,
  1388. .issue_yfs_rpc = yfs_fs_create_file,
  1389. .success = afs_create_success,
  1390. .aborted = afs_check_for_remote_deletion,
  1391. .edit_dir = afs_create_edit_dir,
  1392. .put = afs_create_put,
  1393. };
  1394. /*
  1395. * create a regular file on an AFS filesystem
  1396. */
  1397. static int afs_create(struct mnt_idmap *idmap, struct inode *dir,
  1398. struct dentry *dentry, umode_t mode, bool excl)
  1399. {
  1400. struct afs_operation *op;
  1401. struct afs_vnode *dvnode = AFS_FS_I(dir);
  1402. int ret = -ENAMETOOLONG;
  1403. _enter("{%llx:%llu},{%pd},%ho",
  1404. dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
  1405. if (dentry->d_name.len >= AFSNAMEMAX)
  1406. goto error;
  1407. op = afs_alloc_operation(NULL, dvnode->volume);
  1408. if (IS_ERR(op)) {
  1409. ret = PTR_ERR(op);
  1410. goto error;
  1411. }
  1412. fscache_use_cookie(afs_vnode_cache(dvnode), true);
  1413. afs_op_set_vnode(op, 0, dvnode);
  1414. op->file[0].dv_delta = 1;
  1415. op->file[0].modification = true;
  1416. op->file[0].update_ctime = true;
  1417. op->dentry = dentry;
  1418. op->create.mode = S_IFREG | mode;
  1419. op->create.reason = afs_edit_dir_for_create;
  1420. op->mtime = current_time(dir);
  1421. op->ops = &afs_create_operation;
  1422. ret = afs_do_sync_operation(op);
  1423. afs_dir_unuse_cookie(dvnode, ret);
  1424. return ret;
  1425. error:
  1426. d_drop(dentry);
  1427. _leave(" = %d", ret);
  1428. return ret;
  1429. }
  1430. static void afs_link_success(struct afs_operation *op)
  1431. {
  1432. struct afs_vnode_param *dvp = &op->file[0];
  1433. struct afs_vnode_param *vp = &op->file[1];
  1434. _enter("op=%08x", op->debug_id);
  1435. op->ctime = dvp->scb.status.mtime_client;
  1436. afs_vnode_commit_status(op, dvp);
  1437. afs_vnode_commit_status(op, vp);
  1438. afs_update_dentry_version(op, dvp, op->dentry);
  1439. if (op->dentry_2->d_parent == op->dentry->d_parent)
  1440. afs_update_dentry_version(op, dvp, op->dentry_2);
  1441. ihold(&vp->vnode->netfs.inode);
  1442. d_instantiate(op->dentry, &vp->vnode->netfs.inode);
  1443. }
  1444. static void afs_link_put(struct afs_operation *op)
  1445. {
  1446. _enter("op=%08x", op->debug_id);
  1447. if (afs_op_error(op))
  1448. d_drop(op->dentry);
  1449. }
  1450. static const struct afs_operation_ops afs_link_operation = {
  1451. .issue_afs_rpc = afs_fs_link,
  1452. .issue_yfs_rpc = yfs_fs_link,
  1453. .success = afs_link_success,
  1454. .aborted = afs_check_for_remote_deletion,
  1455. .edit_dir = afs_create_edit_dir,
  1456. .put = afs_link_put,
  1457. };
  1458. /*
  1459. * create a hard link between files in an AFS filesystem
  1460. */
  1461. static int afs_link(struct dentry *from, struct inode *dir,
  1462. struct dentry *dentry)
  1463. {
  1464. struct afs_operation *op;
  1465. struct afs_vnode *dvnode = AFS_FS_I(dir);
  1466. struct afs_vnode *vnode = AFS_FS_I(d_inode(from));
  1467. int ret = -ENAMETOOLONG;
  1468. _enter("{%llx:%llu},{%llx:%llu},{%pd}",
  1469. vnode->fid.vid, vnode->fid.vnode,
  1470. dvnode->fid.vid, dvnode->fid.vnode,
  1471. dentry);
  1472. if (dentry->d_name.len >= AFSNAMEMAX)
  1473. goto error;
  1474. op = afs_alloc_operation(NULL, dvnode->volume);
  1475. if (IS_ERR(op)) {
  1476. ret = PTR_ERR(op);
  1477. goto error;
  1478. }
  1479. fscache_use_cookie(afs_vnode_cache(dvnode), true);
  1480. ret = afs_validate(vnode, op->key);
  1481. if (ret < 0)
  1482. goto error_op;
  1483. afs_op_set_vnode(op, 0, dvnode);
  1484. afs_op_set_vnode(op, 1, vnode);
  1485. op->file[0].dv_delta = 1;
  1486. op->file[0].modification = true;
  1487. op->file[0].update_ctime = true;
  1488. op->file[1].update_ctime = true;
  1489. op->dentry = dentry;
  1490. op->dentry_2 = from;
  1491. op->ops = &afs_link_operation;
  1492. op->create.reason = afs_edit_dir_for_link;
  1493. ret = afs_do_sync_operation(op);
  1494. afs_dir_unuse_cookie(dvnode, ret);
  1495. return ret;
  1496. error_op:
  1497. afs_put_operation(op);
  1498. afs_dir_unuse_cookie(dvnode, ret);
  1499. error:
  1500. d_drop(dentry);
  1501. _leave(" = %d", ret);
  1502. return ret;
  1503. }
  1504. static const struct afs_operation_ops afs_symlink_operation = {
  1505. .issue_afs_rpc = afs_fs_symlink,
  1506. .issue_yfs_rpc = yfs_fs_symlink,
  1507. .success = afs_create_success,
  1508. .aborted = afs_check_for_remote_deletion,
  1509. .edit_dir = afs_create_edit_dir,
  1510. .put = afs_create_put,
  1511. };
  1512. /*
  1513. * create a symlink in an AFS filesystem
  1514. */
  1515. static int afs_symlink(struct mnt_idmap *idmap, struct inode *dir,
  1516. struct dentry *dentry, const char *content)
  1517. {
  1518. struct afs_operation *op;
  1519. struct afs_vnode *dvnode = AFS_FS_I(dir);
  1520. int ret;
  1521. _enter("{%llx:%llu},{%pd},%s",
  1522. dvnode->fid.vid, dvnode->fid.vnode, dentry,
  1523. content);
  1524. ret = -ENAMETOOLONG;
  1525. if (dentry->d_name.len >= AFSNAMEMAX)
  1526. goto error;
  1527. ret = -EINVAL;
  1528. if (strlen(content) >= AFSPATHMAX)
  1529. goto error;
  1530. op = afs_alloc_operation(NULL, dvnode->volume);
  1531. if (IS_ERR(op)) {
  1532. ret = PTR_ERR(op);
  1533. goto error;
  1534. }
  1535. fscache_use_cookie(afs_vnode_cache(dvnode), true);
  1536. afs_op_set_vnode(op, 0, dvnode);
  1537. op->file[0].dv_delta = 1;
  1538. op->dentry = dentry;
  1539. op->ops = &afs_symlink_operation;
  1540. op->create.reason = afs_edit_dir_for_symlink;
  1541. op->create.symlink = content;
  1542. op->mtime = current_time(dir);
  1543. ret = afs_do_sync_operation(op);
  1544. afs_dir_unuse_cookie(dvnode, ret);
  1545. return ret;
  1546. error:
  1547. d_drop(dentry);
  1548. _leave(" = %d", ret);
  1549. return ret;
  1550. }
  1551. static void afs_rename_success(struct afs_operation *op)
  1552. {
  1553. struct afs_vnode *vnode = op->more_files[0].vnode;
  1554. struct afs_vnode *new_vnode = op->more_files[1].vnode;
  1555. _enter("op=%08x", op->debug_id);
  1556. op->ctime = op->file[0].scb.status.mtime_client;
  1557. afs_check_dir_conflict(op, &op->file[1]);
  1558. afs_vnode_commit_status(op, &op->file[0]);
  1559. if (op->file[1].vnode != op->file[0].vnode) {
  1560. op->ctime = op->file[1].scb.status.mtime_client;
  1561. afs_vnode_commit_status(op, &op->file[1]);
  1562. }
  1563. if (op->more_files[0].scb.have_status)
  1564. afs_vnode_commit_status(op, &op->more_files[0]);
  1565. if (op->more_files[1].scb.have_status)
  1566. afs_vnode_commit_status(op, &op->more_files[1]);
  1567. /* If we're moving a subdir between dirs, we need to update
  1568. * its DV counter too as the ".." will be altered.
  1569. */
  1570. if (op->file[0].vnode != op->file[1].vnode) {
  1571. if (S_ISDIR(vnode->netfs.inode.i_mode)) {
  1572. u64 new_dv;
  1573. write_seqlock(&vnode->cb_lock);
  1574. new_dv = vnode->status.data_version + 1;
  1575. trace_afs_set_dv(vnode, new_dv);
  1576. vnode->status.data_version = new_dv;
  1577. inode_set_iversion_raw(&vnode->netfs.inode, new_dv);
  1578. write_sequnlock(&vnode->cb_lock);
  1579. }
  1580. if ((op->rename.rename_flags & RENAME_EXCHANGE) &&
  1581. S_ISDIR(new_vnode->netfs.inode.i_mode)) {
  1582. u64 new_dv;
  1583. write_seqlock(&new_vnode->cb_lock);
  1584. new_dv = new_vnode->status.data_version + 1;
  1585. new_vnode->status.data_version = new_dv;
  1586. inode_set_iversion_raw(&new_vnode->netfs.inode, new_dv);
  1587. write_sequnlock(&new_vnode->cb_lock);
  1588. }
  1589. }
  1590. }
  1591. static void afs_rename_edit_dir(struct afs_operation *op)
  1592. {
  1593. struct netfs_cache_resources orig_cres = {}, new_cres = {};
  1594. struct afs_vnode_param *orig_dvp = &op->file[0];
  1595. struct afs_vnode_param *new_dvp = &op->file[1];
  1596. struct afs_vnode *orig_dvnode = orig_dvp->vnode;
  1597. struct afs_vnode *new_dvnode = new_dvp->vnode;
  1598. struct afs_vnode *vnode = AFS_FS_I(d_inode(op->dentry));
  1599. struct dentry *old_dentry = op->dentry;
  1600. struct dentry *new_dentry = op->dentry_2;
  1601. struct inode *new_inode;
  1602. _enter("op=%08x", op->debug_id);
  1603. if (op->rename.rehash) {
  1604. d_rehash(op->rename.rehash);
  1605. op->rename.rehash = NULL;
  1606. }
  1607. fscache_begin_write_operation(&orig_cres, afs_vnode_cache(orig_dvnode));
  1608. if (new_dvnode != orig_dvnode)
  1609. fscache_begin_write_operation(&new_cres, afs_vnode_cache(new_dvnode));
  1610. down_write(&orig_dvnode->validate_lock);
  1611. if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags) &&
  1612. orig_dvnode->status.data_version == orig_dvp->dv_before + orig_dvp->dv_delta)
  1613. afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
  1614. afs_edit_dir_for_rename_0);
  1615. if (new_dvnode != orig_dvnode) {
  1616. up_write(&orig_dvnode->validate_lock);
  1617. down_write(&new_dvnode->validate_lock);
  1618. }
  1619. if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags) &&
  1620. new_dvnode->status.data_version == new_dvp->dv_before + new_dvp->dv_delta) {
  1621. if (!op->rename.new_negative)
  1622. afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
  1623. afs_edit_dir_for_rename_1);
  1624. afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
  1625. &vnode->fid, afs_edit_dir_for_rename_2);
  1626. }
  1627. if (S_ISDIR(vnode->netfs.inode.i_mode) &&
  1628. new_dvnode != orig_dvnode &&
  1629. test_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
  1630. afs_edit_dir_update(vnode, &dotdot_name, new_dvnode,
  1631. afs_edit_dir_for_rename_sub);
  1632. new_inode = d_inode(new_dentry);
  1633. if (new_inode) {
  1634. spin_lock(&new_inode->i_lock);
  1635. if (S_ISDIR(new_inode->i_mode))
  1636. clear_nlink(new_inode);
  1637. else if (new_inode->i_nlink > 0)
  1638. drop_nlink(new_inode);
  1639. spin_unlock(&new_inode->i_lock);
  1640. }
  1641. /* Now we can update d_fsdata on the dentries to reflect their
  1642. * new parent's data_version.
  1643. */
  1644. afs_update_dentry_version(op, new_dvp, op->dentry);
  1645. afs_update_dentry_version(op, new_dvp, op->dentry_2);
  1646. d_move(old_dentry, new_dentry);
  1647. up_write(&new_dvnode->validate_lock);
  1648. fscache_end_operation(&orig_cres);
  1649. if (new_dvnode != orig_dvnode)
  1650. fscache_end_operation(&new_cres);
  1651. }
  1652. static void afs_rename_exchange_edit_dir(struct afs_operation *op)
  1653. {
  1654. struct afs_vnode_param *orig_dvp = &op->file[0];
  1655. struct afs_vnode_param *new_dvp = &op->file[1];
  1656. struct afs_vnode *orig_dvnode = orig_dvp->vnode;
  1657. struct afs_vnode *new_dvnode = new_dvp->vnode;
  1658. struct afs_vnode *old_vnode = op->more_files[0].vnode;
  1659. struct afs_vnode *new_vnode = op->more_files[1].vnode;
  1660. struct dentry *old_dentry = op->dentry;
  1661. struct dentry *new_dentry = op->dentry_2;
  1662. _enter("op=%08x", op->debug_id);
  1663. if (new_dvnode == orig_dvnode) {
  1664. down_write(&orig_dvnode->validate_lock);
  1665. if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags) &&
  1666. orig_dvnode->status.data_version == orig_dvp->dv_before + orig_dvp->dv_delta) {
  1667. afs_edit_dir_update(orig_dvnode, &old_dentry->d_name,
  1668. new_vnode, afs_edit_dir_for_rename_0);
  1669. afs_edit_dir_update(orig_dvnode, &new_dentry->d_name,
  1670. old_vnode, afs_edit_dir_for_rename_1);
  1671. }
  1672. d_exchange(old_dentry, new_dentry);
  1673. up_write(&orig_dvnode->validate_lock);
  1674. } else {
  1675. down_write(&orig_dvnode->validate_lock);
  1676. if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags) &&
  1677. orig_dvnode->status.data_version == orig_dvp->dv_before + orig_dvp->dv_delta)
  1678. afs_edit_dir_update(orig_dvnode, &old_dentry->d_name,
  1679. new_vnode, afs_edit_dir_for_rename_0);
  1680. up_write(&orig_dvnode->validate_lock);
  1681. down_write(&new_dvnode->validate_lock);
  1682. if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags) &&
  1683. new_dvnode->status.data_version == new_dvp->dv_before + new_dvp->dv_delta)
  1684. afs_edit_dir_update(new_dvnode, &new_dentry->d_name,
  1685. old_vnode, afs_edit_dir_for_rename_1);
  1686. if (S_ISDIR(old_vnode->netfs.inode.i_mode) &&
  1687. test_bit(AFS_VNODE_DIR_VALID, &old_vnode->flags))
  1688. afs_edit_dir_update(old_vnode, &dotdot_name, new_dvnode,
  1689. afs_edit_dir_for_rename_sub);
  1690. if (S_ISDIR(new_vnode->netfs.inode.i_mode) &&
  1691. test_bit(AFS_VNODE_DIR_VALID, &new_vnode->flags))
  1692. afs_edit_dir_update(new_vnode, &dotdot_name, orig_dvnode,
  1693. afs_edit_dir_for_rename_sub);
  1694. /* Now we can update d_fsdata on the dentries to reflect their
  1695. * new parents' data_version.
  1696. */
  1697. afs_update_dentry_version(op, new_dvp, old_dentry);
  1698. afs_update_dentry_version(op, orig_dvp, new_dentry);
  1699. d_exchange(old_dentry, new_dentry);
  1700. up_write(&new_dvnode->validate_lock);
  1701. }
  1702. }
  1703. static void afs_rename_put(struct afs_operation *op)
  1704. {
  1705. _enter("op=%08x", op->debug_id);
  1706. if (op->rename.rehash)
  1707. d_rehash(op->rename.rehash);
  1708. dput(op->rename.tmp);
  1709. if (afs_op_error(op))
  1710. d_rehash(op->dentry);
  1711. }
  1712. static const struct afs_operation_ops afs_rename_operation = {
  1713. .issue_afs_rpc = afs_fs_rename,
  1714. .issue_yfs_rpc = yfs_fs_rename,
  1715. .success = afs_rename_success,
  1716. .edit_dir = afs_rename_edit_dir,
  1717. .put = afs_rename_put,
  1718. };
  1719. #if 0 /* Autoswitched in yfs_fs_rename_replace(). */
  1720. static const struct afs_operation_ops afs_rename_replace_operation = {
  1721. .issue_afs_rpc = NULL,
  1722. .issue_yfs_rpc = yfs_fs_rename_replace,
  1723. .success = afs_rename_success,
  1724. .edit_dir = afs_rename_edit_dir,
  1725. .put = afs_rename_put,
  1726. };
  1727. #endif
  1728. static const struct afs_operation_ops afs_rename_noreplace_operation = {
  1729. .issue_afs_rpc = NULL,
  1730. .issue_yfs_rpc = yfs_fs_rename_noreplace,
  1731. .success = afs_rename_success,
  1732. .edit_dir = afs_rename_edit_dir,
  1733. .put = afs_rename_put,
  1734. };
  1735. static const struct afs_operation_ops afs_rename_exchange_operation = {
  1736. .issue_afs_rpc = NULL,
  1737. .issue_yfs_rpc = yfs_fs_rename_exchange,
  1738. .success = afs_rename_success,
  1739. .edit_dir = afs_rename_exchange_edit_dir,
  1740. .put = afs_rename_put,
  1741. };
  1742. /*
  1743. * rename a file in an AFS filesystem and/or move it between directories
  1744. */
  1745. static int afs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
  1746. struct dentry *old_dentry, struct inode *new_dir,
  1747. struct dentry *new_dentry, unsigned int flags)
  1748. {
  1749. struct afs_operation *op;
  1750. struct afs_vnode *orig_dvnode, *new_dvnode, *vnode, *new_vnode = NULL;
  1751. int ret;
  1752. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
  1753. return -EINVAL;
  1754. /* Don't allow silly-rename files be moved around. */
  1755. if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1756. return -EINVAL;
  1757. vnode = AFS_FS_I(d_inode(old_dentry));
  1758. orig_dvnode = AFS_FS_I(old_dir);
  1759. new_dvnode = AFS_FS_I(new_dir);
  1760. if (d_is_positive(new_dentry))
  1761. new_vnode = AFS_FS_I(d_inode(new_dentry));
  1762. _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
  1763. orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
  1764. vnode->fid.vid, vnode->fid.vnode,
  1765. new_dvnode->fid.vid, new_dvnode->fid.vnode,
  1766. new_dentry);
  1767. op = afs_alloc_operation(NULL, orig_dvnode->volume);
  1768. if (IS_ERR(op))
  1769. return PTR_ERR(op);
  1770. fscache_use_cookie(afs_vnode_cache(orig_dvnode), true);
  1771. if (new_dvnode != orig_dvnode)
  1772. fscache_use_cookie(afs_vnode_cache(new_dvnode), true);
  1773. ret = afs_validate(vnode, op->key);
  1774. afs_op_set_error(op, ret);
  1775. if (ret < 0)
  1776. goto error;
  1777. ret = -ENOMEM;
  1778. op->more_files = kvzalloc_objs(struct afs_vnode_param, 2);
  1779. if (!op->more_files)
  1780. goto error;
  1781. afs_op_set_vnode(op, 0, orig_dvnode);
  1782. afs_op_set_vnode(op, 1, new_dvnode); /* May be same as orig_dvnode */
  1783. op->file[0].dv_delta = 1;
  1784. op->file[1].dv_delta = 1;
  1785. op->file[0].modification = true;
  1786. op->file[1].modification = true;
  1787. op->file[0].update_ctime = true;
  1788. op->file[1].update_ctime = true;
  1789. op->more_files[0].vnode = vnode;
  1790. op->more_files[0].speculative = true;
  1791. op->more_files[1].vnode = new_vnode;
  1792. op->more_files[1].speculative = true;
  1793. op->nr_files = 4;
  1794. op->dentry = old_dentry;
  1795. op->dentry_2 = new_dentry;
  1796. op->rename.rename_flags = flags;
  1797. op->rename.new_negative = d_is_negative(new_dentry);
  1798. if (flags & RENAME_NOREPLACE) {
  1799. op->ops = &afs_rename_noreplace_operation;
  1800. } else if (flags & RENAME_EXCHANGE) {
  1801. op->ops = &afs_rename_exchange_operation;
  1802. d_drop(new_dentry);
  1803. } else {
  1804. /* If we might displace the target, we might need to do silly
  1805. * rename.
  1806. */
  1807. op->ops = &afs_rename_operation;
  1808. /* For non-directories, check whether the target is busy and if
  1809. * so, make a copy of the dentry and then do a silly-rename.
  1810. * If the silly-rename succeeds, the copied dentry is hashed
  1811. * and becomes the new target.
  1812. */
  1813. if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) {
  1814. /* To prevent any new references to the target during
  1815. * the rename, we unhash the dentry in advance.
  1816. */
  1817. if (!d_unhashed(new_dentry)) {
  1818. d_drop(new_dentry);
  1819. op->rename.rehash = new_dentry;
  1820. }
  1821. if (d_count(new_dentry) > 2) {
  1822. /* copy the target dentry's name */
  1823. op->rename.tmp = d_alloc(new_dentry->d_parent,
  1824. &new_dentry->d_name);
  1825. if (!op->rename.tmp) {
  1826. afs_op_nomem(op);
  1827. goto error;
  1828. }
  1829. ret = afs_sillyrename(new_dvnode,
  1830. AFS_FS_I(d_inode(new_dentry)),
  1831. new_dentry, op->key);
  1832. if (ret) {
  1833. afs_op_set_error(op, ret);
  1834. goto error;
  1835. }
  1836. op->dentry_2 = op->rename.tmp;
  1837. op->rename.rehash = NULL;
  1838. op->rename.new_negative = true;
  1839. }
  1840. }
  1841. }
  1842. /* This bit is potentially nasty as there's a potential race with
  1843. * afs_d_revalidate{,_rcu}(). We have to change d_fsdata on the dentry
  1844. * to reflect it's new parent's new data_version after the op, but
  1845. * d_revalidate may see old_dentry between the op having taken place
  1846. * and the version being updated.
  1847. *
  1848. * So drop the old_dentry for now to make other threads go through
  1849. * lookup instead - which we hold a lock against.
  1850. */
  1851. d_drop(old_dentry);
  1852. ret = afs_do_sync_operation(op);
  1853. if (ret == -ENOTSUPP)
  1854. ret = -EINVAL;
  1855. out:
  1856. afs_dir_unuse_cookie(orig_dvnode, ret);
  1857. if (new_dvnode != orig_dvnode)
  1858. afs_dir_unuse_cookie(new_dvnode, ret);
  1859. return ret;
  1860. error:
  1861. ret = afs_put_operation(op);
  1862. goto out;
  1863. }
  1864. /*
  1865. * Write the file contents to the cache as a single blob.
  1866. */
  1867. int afs_single_writepages(struct address_space *mapping,
  1868. struct writeback_control *wbc)
  1869. {
  1870. struct afs_vnode *dvnode = AFS_FS_I(mapping->host);
  1871. struct iov_iter iter;
  1872. bool is_dir = (S_ISDIR(dvnode->netfs.inode.i_mode) &&
  1873. !test_bit(AFS_VNODE_MOUNTPOINT, &dvnode->flags));
  1874. int ret = 0;
  1875. /* Need to lock to prevent the folio queue and folios from being thrown
  1876. * away.
  1877. */
  1878. down_read(&dvnode->validate_lock);
  1879. if (is_dir ?
  1880. test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) :
  1881. atomic64_read(&dvnode->cb_expires_at) != AFS_NO_CB_PROMISE) {
  1882. iov_iter_folio_queue(&iter, ITER_SOURCE, dvnode->directory, 0, 0,
  1883. i_size_read(&dvnode->netfs.inode));
  1884. ret = netfs_writeback_single(mapping, wbc, &iter);
  1885. }
  1886. up_read(&dvnode->validate_lock);
  1887. return ret;
  1888. }