dir.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  1. /* dir.c: AFS filesystem directory handling
  2. *
  3. * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/fs.h>
  13. #include <linux/namei.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/swap.h>
  16. #include <linux/ctype.h>
  17. #include <linux/sched.h>
  18. #include <linux/task_io_accounting_ops.h>
  19. #include "internal.h"
  20. #include "xdr_fs.h"
  21. static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
  22. unsigned int flags);
  23. static int afs_dir_open(struct inode *inode, struct file *file);
  24. static int afs_readdir(struct file *file, struct dir_context *ctx);
  25. static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
  26. static int afs_d_delete(const struct dentry *dentry);
  27. static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
  28. loff_t fpos, u64 ino, unsigned dtype);
  29. static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
  30. loff_t fpos, u64 ino, unsigned dtype);
  31. static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  32. bool excl);
  33. static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
  34. static int afs_rmdir(struct inode *dir, struct dentry *dentry);
  35. static int afs_unlink(struct inode *dir, struct dentry *dentry);
  36. static int afs_link(struct dentry *from, struct inode *dir,
  37. struct dentry *dentry);
  38. static int afs_symlink(struct inode *dir, struct dentry *dentry,
  39. const char *content);
  40. static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
  41. struct inode *new_dir, struct dentry *new_dentry,
  42. unsigned int flags);
  43. static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags);
  44. static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
  45. unsigned int length);
  46. static int afs_dir_set_page_dirty(struct page *page)
  47. {
  48. BUG(); /* This should never happen. */
  49. }
  50. const struct file_operations afs_dir_file_operations = {
  51. .open = afs_dir_open,
  52. .release = afs_release,
  53. .iterate_shared = afs_readdir,
  54. .lock = afs_lock,
  55. .llseek = generic_file_llseek,
  56. };
  57. const struct inode_operations afs_dir_inode_operations = {
  58. .create = afs_create,
  59. .lookup = afs_lookup,
  60. .link = afs_link,
  61. .unlink = afs_unlink,
  62. .symlink = afs_symlink,
  63. .mkdir = afs_mkdir,
  64. .rmdir = afs_rmdir,
  65. .rename = afs_rename,
  66. .permission = afs_permission,
  67. .getattr = afs_getattr,
  68. .setattr = afs_setattr,
  69. .listxattr = afs_listxattr,
  70. };
  71. const struct address_space_operations afs_dir_aops = {
  72. .set_page_dirty = afs_dir_set_page_dirty,
  73. .releasepage = afs_dir_releasepage,
  74. .invalidatepage = afs_dir_invalidatepage,
  75. };
  76. const struct dentry_operations afs_fs_dentry_operations = {
  77. .d_revalidate = afs_d_revalidate,
  78. .d_delete = afs_d_delete,
  79. .d_release = afs_d_release,
  80. .d_automount = afs_d_automount,
  81. };
  82. struct afs_lookup_one_cookie {
  83. struct dir_context ctx;
  84. struct qstr name;
  85. bool found;
  86. struct afs_fid fid;
  87. };
  88. struct afs_lookup_cookie {
  89. struct dir_context ctx;
  90. struct qstr name;
  91. bool found;
  92. bool one_only;
  93. unsigned short nr_fids;
  94. struct afs_file_status *statuses;
  95. struct afs_callback *callbacks;
  96. struct afs_fid fids[50];
  97. };
  98. /*
  99. * check that a directory page is valid
  100. */
  101. static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page,
  102. loff_t i_size)
  103. {
  104. struct afs_xdr_dir_page *dbuf;
  105. loff_t latter, off;
  106. int tmp, qty;
  107. /* Determine how many magic numbers there should be in this page, but
  108. * we must take care because the directory may change size under us.
  109. */
  110. off = page_offset(page);
  111. if (i_size <= off)
  112. goto checked;
  113. latter = i_size - off;
  114. if (latter >= PAGE_SIZE)
  115. qty = PAGE_SIZE;
  116. else
  117. qty = latter;
  118. qty /= sizeof(union afs_xdr_dir_block);
  119. /* check them */
  120. dbuf = kmap(page);
  121. for (tmp = 0; tmp < qty; tmp++) {
  122. if (dbuf->blocks[tmp].hdr.magic != AFS_DIR_MAGIC) {
  123. printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
  124. __func__, dvnode->vfs_inode.i_ino, tmp, qty,
  125. ntohs(dbuf->blocks[tmp].hdr.magic));
  126. trace_afs_dir_check_failed(dvnode, off, i_size);
  127. kunmap(page);
  128. goto error;
  129. }
  130. /* Make sure each block is NUL terminated so we can reasonably
  131. * use string functions on it. The filenames in the page
  132. * *should* be NUL-terminated anyway.
  133. */
  134. ((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0;
  135. }
  136. kunmap(page);
  137. checked:
  138. afs_stat_v(dvnode, n_read_dir);
  139. return true;
  140. error:
  141. return false;
  142. }
  143. /*
  144. * open an AFS directory file
  145. */
  146. static int afs_dir_open(struct inode *inode, struct file *file)
  147. {
  148. _enter("{%lu}", inode->i_ino);
  149. BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
  150. BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
  151. if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
  152. return -ENOENT;
  153. return afs_open(inode, file);
  154. }
  155. /*
  156. * Read the directory into the pagecache in one go, scrubbing the previous
  157. * contents. The list of pages is returned, pinning them so that they don't
  158. * get reclaimed during the iteration.
  159. */
  160. static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
  161. __acquires(&dvnode->validate_lock)
  162. {
  163. struct afs_read *req;
  164. loff_t i_size;
  165. int nr_pages, nr_inline, i, n;
  166. int ret = -ENOMEM;
  167. retry:
  168. i_size = i_size_read(&dvnode->vfs_inode);
  169. if (i_size < 2048)
  170. return ERR_PTR(-EIO);
  171. if (i_size > 2048 * 1024)
  172. return ERR_PTR(-EFBIG);
  173. _enter("%llu", i_size);
  174. /* Get a request record to hold the page list. We want to hold it
  175. * inline if we can, but we don't want to make an order 1 allocation.
  176. */
  177. nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
  178. nr_inline = nr_pages;
  179. if (nr_inline > (PAGE_SIZE - sizeof(*req)) / sizeof(struct page *))
  180. nr_inline = 0;
  181. req = kzalloc(sizeof(*req) + sizeof(struct page *) * nr_inline,
  182. GFP_KERNEL);
  183. if (!req)
  184. return ERR_PTR(-ENOMEM);
  185. refcount_set(&req->usage, 1);
  186. req->nr_pages = nr_pages;
  187. req->actual_len = i_size; /* May change */
  188. req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
  189. req->data_version = dvnode->status.data_version; /* May change */
  190. if (nr_inline > 0) {
  191. req->pages = req->array;
  192. } else {
  193. req->pages = kcalloc(nr_pages, sizeof(struct page *),
  194. GFP_KERNEL);
  195. if (!req->pages)
  196. goto error;
  197. }
  198. /* Get a list of all the pages that hold or will hold the directory
  199. * content. We need to fill in any gaps that we might find where the
  200. * memory reclaimer has been at work. If there are any gaps, we will
  201. * need to reread the entire directory contents.
  202. */
  203. i = 0;
  204. do {
  205. n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i,
  206. req->nr_pages - i,
  207. req->pages + i);
  208. _debug("find %u at %u/%u", n, i, req->nr_pages);
  209. if (n == 0) {
  210. gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask;
  211. if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  212. afs_stat_v(dvnode, n_inval);
  213. ret = -ENOMEM;
  214. req->pages[i] = __page_cache_alloc(gfp);
  215. if (!req->pages[i])
  216. goto error;
  217. ret = add_to_page_cache_lru(req->pages[i],
  218. dvnode->vfs_inode.i_mapping,
  219. i, gfp);
  220. if (ret < 0)
  221. goto error;
  222. set_page_private(req->pages[i], 1);
  223. SetPagePrivate(req->pages[i]);
  224. unlock_page(req->pages[i]);
  225. i++;
  226. } else {
  227. i += n;
  228. }
  229. } while (i < req->nr_pages);
  230. /* If we're going to reload, we need to lock all the pages to prevent
  231. * races.
  232. */
  233. ret = -ERESTARTSYS;
  234. if (down_read_killable(&dvnode->validate_lock) < 0)
  235. goto error;
  236. if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  237. goto success;
  238. up_read(&dvnode->validate_lock);
  239. if (down_write_killable(&dvnode->validate_lock) < 0)
  240. goto error;
  241. if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
  242. ret = afs_fetch_data(dvnode, key, req);
  243. if (ret < 0)
  244. goto error_unlock;
  245. task_io_account_read(PAGE_SIZE * req->nr_pages);
  246. if (req->len < req->file_size)
  247. goto content_has_grown;
  248. /* Validate the data we just read. */
  249. ret = -EIO;
  250. for (i = 0; i < req->nr_pages; i++)
  251. if (!afs_dir_check_page(dvnode, req->pages[i],
  252. req->actual_len))
  253. goto error_unlock;
  254. // TODO: Trim excess pages
  255. set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
  256. }
  257. downgrade_write(&dvnode->validate_lock);
  258. success:
  259. return req;
  260. error_unlock:
  261. up_write(&dvnode->validate_lock);
  262. error:
  263. afs_put_read(req);
  264. _leave(" = %d", ret);
  265. return ERR_PTR(ret);
  266. content_has_grown:
  267. up_write(&dvnode->validate_lock);
  268. afs_put_read(req);
  269. goto retry;
  270. }
  271. /*
  272. * deal with one block in an AFS directory
  273. */
  274. static int afs_dir_iterate_block(struct dir_context *ctx,
  275. union afs_xdr_dir_block *block,
  276. unsigned blkoff)
  277. {
  278. union afs_xdr_dirent *dire;
  279. unsigned offset, next, curr;
  280. size_t nlen;
  281. int tmp;
  282. _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
  283. curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
  284. /* walk through the block, an entry at a time */
  285. for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
  286. offset < AFS_DIR_SLOTS_PER_BLOCK;
  287. offset = next
  288. ) {
  289. next = offset + 1;
  290. /* skip entries marked unused in the bitmap */
  291. if (!(block->hdr.bitmap[offset / 8] &
  292. (1 << (offset % 8)))) {
  293. _debug("ENT[%zu.%u]: unused",
  294. blkoff / sizeof(union afs_xdr_dir_block), offset);
  295. if (offset >= curr)
  296. ctx->pos = blkoff +
  297. next * sizeof(union afs_xdr_dirent);
  298. continue;
  299. }
  300. /* got a valid entry */
  301. dire = &block->dirents[offset];
  302. nlen = strnlen(dire->u.name,
  303. sizeof(*block) -
  304. offset * sizeof(union afs_xdr_dirent));
  305. _debug("ENT[%zu.%u]: %s %zu \"%s\"",
  306. blkoff / sizeof(union afs_xdr_dir_block), offset,
  307. (offset < curr ? "skip" : "fill"),
  308. nlen, dire->u.name);
  309. /* work out where the next possible entry is */
  310. for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_xdr_dirent)) {
  311. if (next >= AFS_DIR_SLOTS_PER_BLOCK) {
  312. _debug("ENT[%zu.%u]:"
  313. " %u travelled beyond end dir block"
  314. " (len %u/%zu)",
  315. blkoff / sizeof(union afs_xdr_dir_block),
  316. offset, next, tmp, nlen);
  317. return -EIO;
  318. }
  319. if (!(block->hdr.bitmap[next / 8] &
  320. (1 << (next % 8)))) {
  321. _debug("ENT[%zu.%u]:"
  322. " %u unmarked extension (len %u/%zu)",
  323. blkoff / sizeof(union afs_xdr_dir_block),
  324. offset, next, tmp, nlen);
  325. return -EIO;
  326. }
  327. _debug("ENT[%zu.%u]: ext %u/%zu",
  328. blkoff / sizeof(union afs_xdr_dir_block),
  329. next, tmp, nlen);
  330. next++;
  331. }
  332. /* skip if starts before the current position */
  333. if (offset < curr)
  334. continue;
  335. /* found the next entry */
  336. if (!dir_emit(ctx, dire->u.name, nlen,
  337. ntohl(dire->u.vnode),
  338. (ctx->actor == afs_lookup_filldir ||
  339. ctx->actor == afs_lookup_one_filldir)?
  340. ntohl(dire->u.unique) : DT_UNKNOWN)) {
  341. _leave(" = 0 [full]");
  342. return 0;
  343. }
  344. ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
  345. }
  346. _leave(" = 1 [more]");
  347. return 1;
  348. }
  349. /*
  350. * iterate through the data blob that lists the contents of an AFS directory
  351. */
  352. static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
  353. struct key *key)
  354. {
  355. struct afs_vnode *dvnode = AFS_FS_I(dir);
  356. struct afs_xdr_dir_page *dbuf;
  357. union afs_xdr_dir_block *dblock;
  358. struct afs_read *req;
  359. struct page *page;
  360. unsigned blkoff, limit;
  361. int ret;
  362. _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
  363. if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
  364. _leave(" = -ESTALE");
  365. return -ESTALE;
  366. }
  367. req = afs_read_dir(dvnode, key);
  368. if (IS_ERR(req))
  369. return PTR_ERR(req);
  370. /* round the file position up to the next entry boundary */
  371. ctx->pos += sizeof(union afs_xdr_dirent) - 1;
  372. ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
  373. /* walk through the blocks in sequence */
  374. ret = 0;
  375. while (ctx->pos < req->actual_len) {
  376. blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1);
  377. /* Fetch the appropriate page from the directory and re-add it
  378. * to the LRU.
  379. */
  380. page = req->pages[blkoff / PAGE_SIZE];
  381. if (!page) {
  382. ret = -EIO;
  383. break;
  384. }
  385. mark_page_accessed(page);
  386. limit = blkoff & ~(PAGE_SIZE - 1);
  387. dbuf = kmap(page);
  388. /* deal with the individual blocks stashed on this page */
  389. do {
  390. dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
  391. sizeof(union afs_xdr_dir_block)];
  392. ret = afs_dir_iterate_block(ctx, dblock, blkoff);
  393. if (ret != 1) {
  394. kunmap(page);
  395. goto out;
  396. }
  397. blkoff += sizeof(union afs_xdr_dir_block);
  398. } while (ctx->pos < dir->i_size && blkoff < limit);
  399. kunmap(page);
  400. ret = 0;
  401. }
  402. out:
  403. up_read(&dvnode->validate_lock);
  404. afs_put_read(req);
  405. _leave(" = %d", ret);
  406. return ret;
  407. }
  408. /*
  409. * read an AFS directory
  410. */
  411. static int afs_readdir(struct file *file, struct dir_context *ctx)
  412. {
  413. return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file));
  414. }
  415. /*
  416. * Search the directory for a single name
  417. * - if afs_dir_iterate_block() spots this function, it'll pass the FID
  418. * uniquifier through dtype
  419. */
  420. static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
  421. int nlen, loff_t fpos, u64 ino, unsigned dtype)
  422. {
  423. struct afs_lookup_one_cookie *cookie =
  424. container_of(ctx, struct afs_lookup_one_cookie, ctx);
  425. _enter("{%s,%u},%s,%u,,%llu,%u",
  426. cookie->name.name, cookie->name.len, name, nlen,
  427. (unsigned long long) ino, dtype);
  428. /* insanity checks first */
  429. BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
  430. BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
  431. if (cookie->name.len != nlen ||
  432. memcmp(cookie->name.name, name, nlen) != 0) {
  433. _leave(" = 0 [no]");
  434. return 0;
  435. }
  436. cookie->fid.vnode = ino;
  437. cookie->fid.unique = dtype;
  438. cookie->found = 1;
  439. _leave(" = -1 [found]");
  440. return -1;
  441. }
  442. /*
  443. * Do a lookup of a single name in a directory
  444. * - just returns the FID the dentry name maps to if found
  445. */
  446. static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
  447. struct afs_fid *fid, struct key *key)
  448. {
  449. struct afs_super_info *as = dir->i_sb->s_fs_info;
  450. struct afs_lookup_one_cookie cookie = {
  451. .ctx.actor = afs_lookup_one_filldir,
  452. .name = dentry->d_name,
  453. .fid.vid = as->volume->vid
  454. };
  455. int ret;
  456. _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
  457. /* search the directory */
  458. ret = afs_dir_iterate(dir, &cookie.ctx, key);
  459. if (ret < 0) {
  460. _leave(" = %d [iter]", ret);
  461. return ret;
  462. }
  463. ret = -ENOENT;
  464. if (!cookie.found) {
  465. _leave(" = -ENOENT [not found]");
  466. return -ENOENT;
  467. }
  468. *fid = cookie.fid;
  469. _leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique);
  470. return 0;
  471. }
  472. /*
  473. * search the directory for a name
  474. * - if afs_dir_iterate_block() spots this function, it'll pass the FID
  475. * uniquifier through dtype
  476. */
  477. static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
  478. int nlen, loff_t fpos, u64 ino, unsigned dtype)
  479. {
  480. struct afs_lookup_cookie *cookie =
  481. container_of(ctx, struct afs_lookup_cookie, ctx);
  482. int ret;
  483. _enter("{%s,%u},%s,%u,,%llu,%u",
  484. cookie->name.name, cookie->name.len, name, nlen,
  485. (unsigned long long) ino, dtype);
  486. /* insanity checks first */
  487. BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
  488. BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
  489. if (cookie->found) {
  490. if (cookie->nr_fids < 50) {
  491. cookie->fids[cookie->nr_fids].vnode = ino;
  492. cookie->fids[cookie->nr_fids].unique = dtype;
  493. cookie->nr_fids++;
  494. }
  495. } else if (cookie->name.len == nlen &&
  496. memcmp(cookie->name.name, name, nlen) == 0) {
  497. cookie->fids[0].vnode = ino;
  498. cookie->fids[0].unique = dtype;
  499. cookie->found = 1;
  500. if (cookie->one_only)
  501. return -1;
  502. }
  503. ret = cookie->nr_fids >= 50 ? -1 : 0;
  504. _leave(" = %d", ret);
  505. return ret;
  506. }
  507. /*
  508. * Do a lookup in a directory. We make use of bulk lookup to query a slew of
  509. * files in one go and create inodes for them. The inode of the file we were
  510. * asked for is returned.
  511. */
  512. static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
  513. struct key *key)
  514. {
  515. struct afs_lookup_cookie *cookie;
  516. struct afs_cb_interest *cbi = NULL;
  517. struct afs_super_info *as = dir->i_sb->s_fs_info;
  518. struct afs_iget_data data;
  519. struct afs_fs_cursor fc;
  520. struct afs_vnode *dvnode = AFS_FS_I(dir);
  521. struct inode *inode = NULL;
  522. int ret, i;
  523. _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
  524. cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
  525. if (!cookie)
  526. return ERR_PTR(-ENOMEM);
  527. cookie->ctx.actor = afs_lookup_filldir;
  528. cookie->name = dentry->d_name;
  529. cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */
  530. read_seqlock_excl(&dvnode->cb_lock);
  531. if (dvnode->cb_interest &&
  532. dvnode->cb_interest->server &&
  533. test_bit(AFS_SERVER_FL_NO_IBULK, &dvnode->cb_interest->server->flags))
  534. cookie->one_only = true;
  535. read_sequnlock_excl(&dvnode->cb_lock);
  536. for (i = 0; i < 50; i++)
  537. cookie->fids[i].vid = as->volume->vid;
  538. /* search the directory */
  539. ret = afs_dir_iterate(dir, &cookie->ctx, key);
  540. if (ret < 0) {
  541. inode = ERR_PTR(ret);
  542. goto out;
  543. }
  544. inode = ERR_PTR(-ENOENT);
  545. if (!cookie->found)
  546. goto out;
  547. /* Check to see if we already have an inode for the primary fid. */
  548. data.volume = dvnode->volume;
  549. data.fid = cookie->fids[0];
  550. inode = ilookup5(dir->i_sb, cookie->fids[0].vnode, afs_iget5_test, &data);
  551. if (inode)
  552. goto out;
  553. /* Need space for examining all the selected files */
  554. inode = ERR_PTR(-ENOMEM);
  555. cookie->statuses = kcalloc(cookie->nr_fids, sizeof(struct afs_file_status),
  556. GFP_KERNEL);
  557. if (!cookie->statuses)
  558. goto out;
  559. cookie->callbacks = kcalloc(cookie->nr_fids, sizeof(struct afs_callback),
  560. GFP_KERNEL);
  561. if (!cookie->callbacks)
  562. goto out_s;
  563. /* Try FS.InlineBulkStatus first. Abort codes for the individual
  564. * lookups contained therein are stored in the reply without aborting
  565. * the whole operation.
  566. */
  567. if (cookie->one_only)
  568. goto no_inline_bulk_status;
  569. inode = ERR_PTR(-ERESTARTSYS);
  570. if (afs_begin_vnode_operation(&fc, dvnode, key)) {
  571. while (afs_select_fileserver(&fc)) {
  572. if (test_bit(AFS_SERVER_FL_NO_IBULK,
  573. &fc.cbi->server->flags)) {
  574. fc.ac.abort_code = RX_INVALID_OPERATION;
  575. fc.ac.error = -ECONNABORTED;
  576. break;
  577. }
  578. afs_fs_inline_bulk_status(&fc,
  579. afs_v2net(dvnode),
  580. cookie->fids,
  581. cookie->statuses,
  582. cookie->callbacks,
  583. cookie->nr_fids, NULL);
  584. }
  585. if (fc.ac.error == 0)
  586. cbi = afs_get_cb_interest(fc.cbi);
  587. if (fc.ac.abort_code == RX_INVALID_OPERATION)
  588. set_bit(AFS_SERVER_FL_NO_IBULK, &fc.cbi->server->flags);
  589. inode = ERR_PTR(afs_end_vnode_operation(&fc));
  590. }
  591. if (!IS_ERR(inode))
  592. goto success;
  593. if (fc.ac.abort_code != RX_INVALID_OPERATION)
  594. goto out_c;
  595. no_inline_bulk_status:
  596. /* We could try FS.BulkStatus next, but this aborts the entire op if
  597. * any of the lookups fails - so, for the moment, revert to
  598. * FS.FetchStatus for just the primary fid.
  599. */
  600. cookie->nr_fids = 1;
  601. inode = ERR_PTR(-ERESTARTSYS);
  602. if (afs_begin_vnode_operation(&fc, dvnode, key)) {
  603. while (afs_select_fileserver(&fc)) {
  604. afs_fs_fetch_status(&fc,
  605. afs_v2net(dvnode),
  606. cookie->fids,
  607. cookie->statuses,
  608. cookie->callbacks,
  609. NULL);
  610. }
  611. if (fc.ac.error == 0)
  612. cbi = afs_get_cb_interest(fc.cbi);
  613. inode = ERR_PTR(afs_end_vnode_operation(&fc));
  614. }
  615. if (IS_ERR(inode))
  616. goto out_c;
  617. for (i = 0; i < cookie->nr_fids; i++)
  618. cookie->statuses[i].abort_code = 0;
  619. success:
  620. /* Turn all the files into inodes and save the first one - which is the
  621. * one we actually want.
  622. */
  623. if (cookie->statuses[0].abort_code != 0)
  624. inode = ERR_PTR(afs_abort_to_error(cookie->statuses[0].abort_code));
  625. for (i = 0; i < cookie->nr_fids; i++) {
  626. struct inode *ti;
  627. if (cookie->statuses[i].abort_code != 0)
  628. continue;
  629. ti = afs_iget(dir->i_sb, key, &cookie->fids[i],
  630. &cookie->statuses[i],
  631. &cookie->callbacks[i],
  632. cbi);
  633. if (i == 0) {
  634. inode = ti;
  635. } else {
  636. if (!IS_ERR(ti))
  637. iput(ti);
  638. }
  639. }
  640. out_c:
  641. afs_put_cb_interest(afs_v2net(dvnode), cbi);
  642. kfree(cookie->callbacks);
  643. out_s:
  644. kfree(cookie->statuses);
  645. out:
  646. kfree(cookie);
  647. return inode;
  648. }
  649. /*
  650. * Look up an entry in a directory with @sys substitution.
  651. */
  652. static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
  653. struct key *key)
  654. {
  655. struct afs_sysnames *subs;
  656. struct afs_net *net = afs_i2net(dir);
  657. struct dentry *ret;
  658. char *buf, *p, *name;
  659. int len, i;
  660. _enter("");
  661. ret = ERR_PTR(-ENOMEM);
  662. p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
  663. if (!buf)
  664. goto out_p;
  665. if (dentry->d_name.len > 4) {
  666. memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
  667. p += dentry->d_name.len - 4;
  668. }
  669. /* There is an ordered list of substitutes that we have to try. */
  670. read_lock(&net->sysnames_lock);
  671. subs = net->sysnames;
  672. refcount_inc(&subs->usage);
  673. read_unlock(&net->sysnames_lock);
  674. for (i = 0; i < subs->nr; i++) {
  675. name = subs->subs[i];
  676. len = dentry->d_name.len - 4 + strlen(name);
  677. if (len >= AFSNAMEMAX) {
  678. ret = ERR_PTR(-ENAMETOOLONG);
  679. goto out_s;
  680. }
  681. strcpy(p, name);
  682. ret = lookup_one_len(buf, dentry->d_parent, len);
  683. if (IS_ERR(ret) || d_is_positive(ret))
  684. goto out_s;
  685. dput(ret);
  686. }
  687. /* We don't want to d_add() the @sys dentry here as we don't want to
  688. * the cached dentry to hide changes to the sysnames list.
  689. */
  690. ret = NULL;
  691. out_s:
  692. afs_put_sysnames(subs);
  693. kfree(buf);
  694. out_p:
  695. key_put(key);
  696. return ret;
  697. }
  698. /*
  699. * look up an entry in a directory
  700. */
  701. static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
  702. unsigned int flags)
  703. {
  704. struct afs_vnode *dvnode = AFS_FS_I(dir);
  705. struct inode *inode;
  706. struct dentry *d;
  707. struct key *key;
  708. int ret;
  709. _enter("{%x:%u},%p{%pd},",
  710. dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
  711. ASSERTCMP(d_inode(dentry), ==, NULL);
  712. if (dentry->d_name.len >= AFSNAMEMAX) {
  713. _leave(" = -ENAMETOOLONG");
  714. return ERR_PTR(-ENAMETOOLONG);
  715. }
  716. if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
  717. _leave(" = -ESTALE");
  718. return ERR_PTR(-ESTALE);
  719. }
  720. key = afs_request_key(dvnode->volume->cell);
  721. if (IS_ERR(key)) {
  722. _leave(" = %ld [key]", PTR_ERR(key));
  723. return ERR_CAST(key);
  724. }
  725. ret = afs_validate(dvnode, key);
  726. if (ret < 0) {
  727. key_put(key);
  728. _leave(" = %d [val]", ret);
  729. return ERR_PTR(ret);
  730. }
  731. if (dentry->d_name.len >= 4 &&
  732. dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
  733. dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
  734. dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
  735. dentry->d_name.name[dentry->d_name.len - 1] == 's')
  736. return afs_lookup_atsys(dir, dentry, key);
  737. afs_stat_v(dvnode, n_lookup);
  738. inode = afs_do_lookup(dir, dentry, key);
  739. key_put(key);
  740. if (inode == ERR_PTR(-ENOENT)) {
  741. inode = afs_try_auto_mntpt(dentry, dir);
  742. } else {
  743. dentry->d_fsdata =
  744. (void *)(unsigned long)dvnode->status.data_version;
  745. }
  746. d = d_splice_alias(inode, dentry);
  747. if (!IS_ERR_OR_NULL(d))
  748. d->d_fsdata = dentry->d_fsdata;
  749. return d;
  750. }
  751. /*
  752. * check that a dentry lookup hit has found a valid entry
  753. * - NOTE! the hit can be a negative hit too, so we can't assume we have an
  754. * inode
  755. */
  756. static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
  757. {
  758. struct afs_vnode *vnode, *dir;
  759. struct afs_fid uninitialized_var(fid);
  760. struct dentry *parent;
  761. struct inode *inode;
  762. struct key *key;
  763. long dir_version, de_version;
  764. int ret;
  765. if (flags & LOOKUP_RCU)
  766. return -ECHILD;
  767. if (d_really_is_positive(dentry)) {
  768. vnode = AFS_FS_I(d_inode(dentry));
  769. _enter("{v={%x:%u} n=%pd fl=%lx},",
  770. vnode->fid.vid, vnode->fid.vnode, dentry,
  771. vnode->flags);
  772. } else {
  773. _enter("{neg n=%pd}", dentry);
  774. }
  775. key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
  776. if (IS_ERR(key))
  777. key = NULL;
  778. if (d_really_is_positive(dentry)) {
  779. inode = d_inode(dentry);
  780. if (inode) {
  781. vnode = AFS_FS_I(inode);
  782. afs_validate(vnode, key);
  783. if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
  784. goto out_bad;
  785. }
  786. }
  787. /* lock down the parent dentry so we can peer at it */
  788. parent = dget_parent(dentry);
  789. dir = AFS_FS_I(d_inode(parent));
  790. /* validate the parent directory */
  791. afs_validate(dir, key);
  792. if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
  793. _debug("%pd: parent dir deleted", dentry);
  794. goto out_bad_parent;
  795. }
  796. /* We only need to invalidate a dentry if the server's copy changed
  797. * behind our back. If we made the change, it's no problem. Note that
  798. * on a 32-bit system, we only have 32 bits in the dentry to store the
  799. * version.
  800. */
  801. dir_version = (long)dir->status.data_version;
  802. de_version = (long)dentry->d_fsdata;
  803. if (de_version == dir_version)
  804. goto out_valid_noupdate;
  805. dir_version = (long)dir->invalid_before;
  806. if (de_version - dir_version >= 0)
  807. goto out_valid;
  808. _debug("dir modified");
  809. afs_stat_v(dir, n_reval);
  810. /* search the directory for this vnode */
  811. ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key);
  812. switch (ret) {
  813. case 0:
  814. /* the filename maps to something */
  815. if (d_really_is_negative(dentry))
  816. goto out_bad_parent;
  817. inode = d_inode(dentry);
  818. if (is_bad_inode(inode)) {
  819. printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
  820. dentry);
  821. goto out_bad_parent;
  822. }
  823. vnode = AFS_FS_I(inode);
  824. /* if the vnode ID has changed, then the dirent points to a
  825. * different file */
  826. if (fid.vnode != vnode->fid.vnode) {
  827. _debug("%pd: dirent changed [%u != %u]",
  828. dentry, fid.vnode,
  829. vnode->fid.vnode);
  830. goto not_found;
  831. }
  832. /* if the vnode ID uniqifier has changed, then the file has
  833. * been deleted and replaced, and the original vnode ID has
  834. * been reused */
  835. if (fid.unique != vnode->fid.unique) {
  836. _debug("%pd: file deleted (uq %u -> %u I:%u)",
  837. dentry, fid.unique,
  838. vnode->fid.unique,
  839. vnode->vfs_inode.i_generation);
  840. write_seqlock(&vnode->cb_lock);
  841. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  842. write_sequnlock(&vnode->cb_lock);
  843. goto not_found;
  844. }
  845. goto out_valid;
  846. case -ENOENT:
  847. /* the filename is unknown */
  848. _debug("%pd: dirent not found", dentry);
  849. if (d_really_is_positive(dentry))
  850. goto not_found;
  851. goto out_valid;
  852. default:
  853. _debug("failed to iterate dir %pd: %d",
  854. parent, ret);
  855. goto out_bad_parent;
  856. }
  857. out_valid:
  858. dentry->d_fsdata = (void *)dir_version;
  859. out_valid_noupdate:
  860. dput(parent);
  861. key_put(key);
  862. _leave(" = 1 [valid]");
  863. return 1;
  864. /* the dirent, if it exists, now points to a different vnode */
  865. not_found:
  866. spin_lock(&dentry->d_lock);
  867. dentry->d_flags |= DCACHE_NFSFS_RENAMED;
  868. spin_unlock(&dentry->d_lock);
  869. out_bad_parent:
  870. _debug("dropping dentry %pd2", dentry);
  871. dput(parent);
  872. out_bad:
  873. key_put(key);
  874. _leave(" = 0 [bad]");
  875. return 0;
  876. }
  877. /*
  878. * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
  879. * sleep)
  880. * - called from dput() when d_count is going to 0.
  881. * - return 1 to request dentry be unhashed, 0 otherwise
  882. */
  883. static int afs_d_delete(const struct dentry *dentry)
  884. {
  885. _enter("%pd", dentry);
  886. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  887. goto zap;
  888. if (d_really_is_positive(dentry) &&
  889. (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) ||
  890. test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
  891. goto zap;
  892. _leave(" = 0 [keep]");
  893. return 0;
  894. zap:
  895. _leave(" = 1 [zap]");
  896. return 1;
  897. }
  898. /*
  899. * handle dentry release
  900. */
  901. void afs_d_release(struct dentry *dentry)
  902. {
  903. _enter("%pd", dentry);
  904. }
  905. /*
  906. * Create a new inode for create/mkdir/symlink
  907. */
  908. static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
  909. struct dentry *new_dentry,
  910. struct afs_fid *newfid,
  911. struct afs_file_status *newstatus,
  912. struct afs_callback *newcb)
  913. {
  914. struct afs_vnode *vnode;
  915. struct inode *inode;
  916. if (fc->ac.error < 0)
  917. return;
  918. d_drop(new_dentry);
  919. inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
  920. newfid, newstatus, newcb, fc->cbi);
  921. if (IS_ERR(inode)) {
  922. /* ENOMEM or EINTR at a really inconvenient time - just abandon
  923. * the new directory on the server.
  924. */
  925. fc->ac.error = PTR_ERR(inode);
  926. return;
  927. }
  928. vnode = AFS_FS_I(inode);
  929. set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
  930. d_add(new_dentry, inode);
  931. }
  932. /*
  933. * create a directory on an AFS filesystem
  934. */
  935. static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  936. {
  937. struct afs_file_status newstatus;
  938. struct afs_fs_cursor fc;
  939. struct afs_callback newcb;
  940. struct afs_vnode *dvnode = AFS_FS_I(dir);
  941. struct afs_fid newfid;
  942. struct key *key;
  943. u64 data_version = dvnode->status.data_version;
  944. int ret;
  945. mode |= S_IFDIR;
  946. _enter("{%x:%u},{%pd},%ho",
  947. dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
  948. key = afs_request_key(dvnode->volume->cell);
  949. if (IS_ERR(key)) {
  950. ret = PTR_ERR(key);
  951. goto error;
  952. }
  953. ret = -ERESTARTSYS;
  954. if (afs_begin_vnode_operation(&fc, dvnode, key)) {
  955. while (afs_select_fileserver(&fc)) {
  956. fc.cb_break = afs_calc_vnode_cb_break(dvnode);
  957. afs_fs_create(&fc, dentry->d_name.name, mode, data_version,
  958. &newfid, &newstatus, &newcb);
  959. }
  960. afs_check_for_remote_deletion(&fc, fc.vnode);
  961. afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
  962. afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
  963. ret = afs_end_vnode_operation(&fc);
  964. if (ret < 0)
  965. goto error_key;
  966. } else {
  967. goto error_key;
  968. }
  969. if (ret == 0 &&
  970. test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  971. afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
  972. afs_edit_dir_for_create);
  973. key_put(key);
  974. _leave(" = 0");
  975. return 0;
  976. error_key:
  977. key_put(key);
  978. error:
  979. d_drop(dentry);
  980. _leave(" = %d", ret);
  981. return ret;
  982. }
  983. /*
  984. * Remove a subdir from a directory.
  985. */
  986. static void afs_dir_remove_subdir(struct dentry *dentry)
  987. {
  988. if (d_really_is_positive(dentry)) {
  989. struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
  990. clear_nlink(&vnode->vfs_inode);
  991. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  992. clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
  993. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  994. }
  995. }
  996. /*
  997. * remove a directory from an AFS filesystem
  998. */
  999. static int afs_rmdir(struct inode *dir, struct dentry *dentry)
  1000. {
  1001. struct afs_fs_cursor fc;
  1002. struct afs_vnode *dvnode = AFS_FS_I(dir);
  1003. struct key *key;
  1004. u64 data_version = dvnode->status.data_version;
  1005. int ret;
  1006. _enter("{%x:%u},{%pd}",
  1007. dvnode->fid.vid, dvnode->fid.vnode, dentry);
  1008. key = afs_request_key(dvnode->volume->cell);
  1009. if (IS_ERR(key)) {
  1010. ret = PTR_ERR(key);
  1011. goto error;
  1012. }
  1013. ret = -ERESTARTSYS;
  1014. if (afs_begin_vnode_operation(&fc, dvnode, key)) {
  1015. while (afs_select_fileserver(&fc)) {
  1016. fc.cb_break = afs_calc_vnode_cb_break(dvnode);
  1017. afs_fs_remove(&fc, dentry->d_name.name, true,
  1018. data_version);
  1019. }
  1020. afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
  1021. ret = afs_end_vnode_operation(&fc);
  1022. if (ret == 0) {
  1023. afs_dir_remove_subdir(dentry);
  1024. if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  1025. afs_edit_dir_remove(dvnode, &dentry->d_name,
  1026. afs_edit_dir_for_rmdir);
  1027. }
  1028. }
  1029. key_put(key);
  1030. error:
  1031. return ret;
  1032. }
  1033. /*
  1034. * Remove a link to a file or symlink from a directory.
  1035. *
  1036. * If the file was not deleted due to excess hard links, the fileserver will
  1037. * break the callback promise on the file - if it had one - before it returns
  1038. * to us, and if it was deleted, it won't
  1039. *
  1040. * However, if we didn't have a callback promise outstanding, or it was
  1041. * outstanding on a different server, then it won't break it either...
  1042. */
  1043. static int afs_dir_remove_link(struct dentry *dentry, struct key *key,
  1044. unsigned long d_version_before,
  1045. unsigned long d_version_after)
  1046. {
  1047. bool dir_valid;
  1048. int ret = 0;
  1049. /* There were no intervening changes on the server if the version
  1050. * number we got back was incremented by exactly 1.
  1051. */
  1052. dir_valid = (d_version_after == d_version_before + 1);
  1053. if (d_really_is_positive(dentry)) {
  1054. struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
  1055. if (dir_valid) {
  1056. drop_nlink(&vnode->vfs_inode);
  1057. if (vnode->vfs_inode.i_nlink == 0) {
  1058. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  1059. clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
  1060. }
  1061. ret = 0;
  1062. } else {
  1063. clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
  1064. if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
  1065. kdebug("AFS_VNODE_DELETED");
  1066. ret = afs_validate(vnode, key);
  1067. if (ret == -ESTALE)
  1068. ret = 0;
  1069. }
  1070. _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
  1071. }
  1072. return ret;
  1073. }
  1074. /*
  1075. * Remove a file or symlink from an AFS filesystem.
  1076. */
  1077. static int afs_unlink(struct inode *dir, struct dentry *dentry)
  1078. {
  1079. struct afs_fs_cursor fc;
  1080. struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode;
  1081. struct key *key;
  1082. unsigned long d_version = (unsigned long)dentry->d_fsdata;
  1083. u64 data_version = dvnode->status.data_version;
  1084. int ret;
  1085. _enter("{%x:%u},{%pd}",
  1086. dvnode->fid.vid, dvnode->fid.vnode, dentry);
  1087. if (dentry->d_name.len >= AFSNAMEMAX)
  1088. return -ENAMETOOLONG;
  1089. key = afs_request_key(dvnode->volume->cell);
  1090. if (IS_ERR(key)) {
  1091. ret = PTR_ERR(key);
  1092. goto error;
  1093. }
  1094. /* Try to make sure we have a callback promise on the victim. */
  1095. if (d_really_is_positive(dentry)) {
  1096. vnode = AFS_FS_I(d_inode(dentry));
  1097. ret = afs_validate(vnode, key);
  1098. if (ret < 0)
  1099. goto error_key;
  1100. }
  1101. ret = -ERESTARTSYS;
  1102. if (afs_begin_vnode_operation(&fc, dvnode, key)) {
  1103. while (afs_select_fileserver(&fc)) {
  1104. fc.cb_break = afs_calc_vnode_cb_break(dvnode);
  1105. afs_fs_remove(&fc, dentry->d_name.name, false,
  1106. data_version);
  1107. }
  1108. afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
  1109. ret = afs_end_vnode_operation(&fc);
  1110. if (ret == 0)
  1111. ret = afs_dir_remove_link(
  1112. dentry, key, d_version,
  1113. (unsigned long)dvnode->status.data_version);
  1114. if (ret == 0 &&
  1115. test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  1116. afs_edit_dir_remove(dvnode, &dentry->d_name,
  1117. afs_edit_dir_for_unlink);
  1118. }
  1119. error_key:
  1120. key_put(key);
  1121. error:
  1122. _leave(" = %d", ret);
  1123. return ret;
  1124. }
  1125. /*
  1126. * create a regular file on an AFS filesystem
  1127. */
  1128. static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  1129. bool excl)
  1130. {
  1131. struct afs_fs_cursor fc;
  1132. struct afs_file_status newstatus;
  1133. struct afs_callback newcb;
  1134. struct afs_vnode *dvnode = AFS_FS_I(dir);
  1135. struct afs_fid newfid;
  1136. struct key *key;
  1137. u64 data_version = dvnode->status.data_version;
  1138. int ret;
  1139. mode |= S_IFREG;
  1140. _enter("{%x:%u},{%pd},%ho,",
  1141. dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
  1142. ret = -ENAMETOOLONG;
  1143. if (dentry->d_name.len >= AFSNAMEMAX)
  1144. goto error;
  1145. key = afs_request_key(dvnode->volume->cell);
  1146. if (IS_ERR(key)) {
  1147. ret = PTR_ERR(key);
  1148. goto error;
  1149. }
  1150. ret = -ERESTARTSYS;
  1151. if (afs_begin_vnode_operation(&fc, dvnode, key)) {
  1152. while (afs_select_fileserver(&fc)) {
  1153. fc.cb_break = afs_calc_vnode_cb_break(dvnode);
  1154. afs_fs_create(&fc, dentry->d_name.name, mode, data_version,
  1155. &newfid, &newstatus, &newcb);
  1156. }
  1157. afs_check_for_remote_deletion(&fc, fc.vnode);
  1158. afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
  1159. afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
  1160. ret = afs_end_vnode_operation(&fc);
  1161. if (ret < 0)
  1162. goto error_key;
  1163. } else {
  1164. goto error_key;
  1165. }
  1166. if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  1167. afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
  1168. afs_edit_dir_for_create);
  1169. key_put(key);
  1170. _leave(" = 0");
  1171. return 0;
  1172. error_key:
  1173. key_put(key);
  1174. error:
  1175. d_drop(dentry);
  1176. _leave(" = %d", ret);
  1177. return ret;
  1178. }
  1179. /*
  1180. * create a hard link between files in an AFS filesystem
  1181. */
  1182. static int afs_link(struct dentry *from, struct inode *dir,
  1183. struct dentry *dentry)
  1184. {
  1185. struct afs_fs_cursor fc;
  1186. struct afs_vnode *dvnode, *vnode;
  1187. struct key *key;
  1188. u64 data_version;
  1189. int ret;
  1190. vnode = AFS_FS_I(d_inode(from));
  1191. dvnode = AFS_FS_I(dir);
  1192. data_version = dvnode->status.data_version;
  1193. _enter("{%x:%u},{%x:%u},{%pd}",
  1194. vnode->fid.vid, vnode->fid.vnode,
  1195. dvnode->fid.vid, dvnode->fid.vnode,
  1196. dentry);
  1197. ret = -ENAMETOOLONG;
  1198. if (dentry->d_name.len >= AFSNAMEMAX)
  1199. goto error;
  1200. key = afs_request_key(dvnode->volume->cell);
  1201. if (IS_ERR(key)) {
  1202. ret = PTR_ERR(key);
  1203. goto error;
  1204. }
  1205. ret = -ERESTARTSYS;
  1206. if (afs_begin_vnode_operation(&fc, dvnode, key)) {
  1207. if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
  1208. afs_end_vnode_operation(&fc);
  1209. goto error_key;
  1210. }
  1211. while (afs_select_fileserver(&fc)) {
  1212. fc.cb_break = afs_calc_vnode_cb_break(dvnode);
  1213. fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
  1214. afs_fs_link(&fc, vnode, dentry->d_name.name, data_version);
  1215. }
  1216. afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
  1217. afs_vnode_commit_status(&fc, vnode, fc.cb_break_2);
  1218. ihold(&vnode->vfs_inode);
  1219. d_instantiate(dentry, &vnode->vfs_inode);
  1220. mutex_unlock(&vnode->io_lock);
  1221. ret = afs_end_vnode_operation(&fc);
  1222. if (ret < 0)
  1223. goto error_key;
  1224. } else {
  1225. goto error_key;
  1226. }
  1227. if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  1228. afs_edit_dir_add(dvnode, &dentry->d_name, &vnode->fid,
  1229. afs_edit_dir_for_link);
  1230. key_put(key);
  1231. _leave(" = 0");
  1232. return 0;
  1233. error_key:
  1234. key_put(key);
  1235. error:
  1236. d_drop(dentry);
  1237. _leave(" = %d", ret);
  1238. return ret;
  1239. }
  1240. /*
  1241. * create a symlink in an AFS filesystem
  1242. */
  1243. static int afs_symlink(struct inode *dir, struct dentry *dentry,
  1244. const char *content)
  1245. {
  1246. struct afs_fs_cursor fc;
  1247. struct afs_file_status newstatus;
  1248. struct afs_vnode *dvnode = AFS_FS_I(dir);
  1249. struct afs_fid newfid;
  1250. struct key *key;
  1251. u64 data_version = dvnode->status.data_version;
  1252. int ret;
  1253. _enter("{%x:%u},{%pd},%s",
  1254. dvnode->fid.vid, dvnode->fid.vnode, dentry,
  1255. content);
  1256. ret = -ENAMETOOLONG;
  1257. if (dentry->d_name.len >= AFSNAMEMAX)
  1258. goto error;
  1259. ret = -EINVAL;
  1260. if (strlen(content) >= AFSPATHMAX)
  1261. goto error;
  1262. key = afs_request_key(dvnode->volume->cell);
  1263. if (IS_ERR(key)) {
  1264. ret = PTR_ERR(key);
  1265. goto error;
  1266. }
  1267. ret = -ERESTARTSYS;
  1268. if (afs_begin_vnode_operation(&fc, dvnode, key)) {
  1269. while (afs_select_fileserver(&fc)) {
  1270. fc.cb_break = afs_calc_vnode_cb_break(dvnode);
  1271. afs_fs_symlink(&fc, dentry->d_name.name,
  1272. content, data_version,
  1273. &newfid, &newstatus);
  1274. }
  1275. afs_check_for_remote_deletion(&fc, fc.vnode);
  1276. afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
  1277. afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, NULL);
  1278. ret = afs_end_vnode_operation(&fc);
  1279. if (ret < 0)
  1280. goto error_key;
  1281. } else {
  1282. goto error_key;
  1283. }
  1284. if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  1285. afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
  1286. afs_edit_dir_for_symlink);
  1287. key_put(key);
  1288. _leave(" = 0");
  1289. return 0;
  1290. error_key:
  1291. key_put(key);
  1292. error:
  1293. d_drop(dentry);
  1294. _leave(" = %d", ret);
  1295. return ret;
  1296. }
  1297. /*
  1298. * rename a file in an AFS filesystem and/or move it between directories
  1299. */
  1300. static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1301. struct inode *new_dir, struct dentry *new_dentry,
  1302. unsigned int flags)
  1303. {
  1304. struct afs_fs_cursor fc;
  1305. struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
  1306. struct key *key;
  1307. u64 orig_data_version, new_data_version;
  1308. bool new_negative = d_is_negative(new_dentry);
  1309. int ret;
  1310. if (flags)
  1311. return -EINVAL;
  1312. vnode = AFS_FS_I(d_inode(old_dentry));
  1313. orig_dvnode = AFS_FS_I(old_dir);
  1314. new_dvnode = AFS_FS_I(new_dir);
  1315. orig_data_version = orig_dvnode->status.data_version;
  1316. new_data_version = new_dvnode->status.data_version;
  1317. _enter("{%x:%u},{%x:%u},{%x:%u},{%pd}",
  1318. orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
  1319. vnode->fid.vid, vnode->fid.vnode,
  1320. new_dvnode->fid.vid, new_dvnode->fid.vnode,
  1321. new_dentry);
  1322. key = afs_request_key(orig_dvnode->volume->cell);
  1323. if (IS_ERR(key)) {
  1324. ret = PTR_ERR(key);
  1325. goto error;
  1326. }
  1327. ret = -ERESTARTSYS;
  1328. if (afs_begin_vnode_operation(&fc, orig_dvnode, key)) {
  1329. if (orig_dvnode != new_dvnode) {
  1330. if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
  1331. afs_end_vnode_operation(&fc);
  1332. goto error_key;
  1333. }
  1334. }
  1335. while (afs_select_fileserver(&fc)) {
  1336. fc.cb_break = afs_calc_vnode_cb_break(orig_dvnode);
  1337. fc.cb_break_2 = afs_calc_vnode_cb_break(new_dvnode);
  1338. afs_fs_rename(&fc, old_dentry->d_name.name,
  1339. new_dvnode, new_dentry->d_name.name,
  1340. orig_data_version, new_data_version);
  1341. }
  1342. afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break);
  1343. afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2);
  1344. if (orig_dvnode != new_dvnode)
  1345. mutex_unlock(&new_dvnode->io_lock);
  1346. ret = afs_end_vnode_operation(&fc);
  1347. if (ret < 0)
  1348. goto error_key;
  1349. }
  1350. if (ret == 0) {
  1351. if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags))
  1352. afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
  1353. afs_edit_dir_for_rename);
  1354. if (!new_negative &&
  1355. test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
  1356. afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
  1357. afs_edit_dir_for_rename);
  1358. if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
  1359. afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
  1360. &vnode->fid, afs_edit_dir_for_rename);
  1361. }
  1362. error_key:
  1363. key_put(key);
  1364. error:
  1365. _leave(" = %d", ret);
  1366. return ret;
  1367. }
  1368. /*
  1369. * Release a directory page and clean up its private state if it's not busy
  1370. * - return true if the page can now be released, false if not
  1371. */
  1372. static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
  1373. {
  1374. struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
  1375. _enter("{{%x:%u}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
  1376. set_page_private(page, 0);
  1377. ClearPagePrivate(page);
  1378. /* The directory will need reloading. */
  1379. if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  1380. afs_stat_v(dvnode, n_relpg);
  1381. return 1;
  1382. }
  1383. /*
  1384. * invalidate part or all of a page
  1385. * - release a page and clean up its private data if offset is 0 (indicating
  1386. * the entire page)
  1387. */
  1388. static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
  1389. unsigned int length)
  1390. {
  1391. struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
  1392. _enter("{%lu},%u,%u", page->index, offset, length);
  1393. BUG_ON(!PageLocked(page));
  1394. /* The directory will need reloading. */
  1395. if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  1396. afs_stat_v(dvnode, n_inval);
  1397. /* we clean up only if the entire page is being invalidated */
  1398. if (offset == 0 && length == PAGE_SIZE) {
  1399. set_page_private(page, 0);
  1400. ClearPagePrivate(page);
  1401. }
  1402. }