dir.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288
  1. /*
  2. * linux/fs/nfs/dir.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. *
  6. * nfs directory handling functions
  7. *
  8. * 10 Apr 1996 Added silly rename for unlink --okir
  9. * 28 Sep 1996 Improved directory cache --okir
  10. * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de
  11. * Re-implemented silly rename for unlink, newly implemented
  12. * silly rename for nfs_rename() following the suggestions
  13. * of Olaf Kirch (okir) found in this file.
  14. * Following Linus comments on my original hack, this version
  15. * depends only on the dcache stuff and doesn't touch the inode
  16. * layer (iput() and friends).
  17. * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM
  18. */
  19. #include <linux/module.h>
  20. #include <linux/time.h>
  21. #include <linux/errno.h>
  22. #include <linux/stat.h>
  23. #include <linux/fcntl.h>
  24. #include <linux/string.h>
  25. #include <linux/kernel.h>
  26. #include <linux/slab.h>
  27. #include <linux/mm.h>
  28. #include <linux/sunrpc/clnt.h>
  29. #include <linux/nfs_fs.h>
  30. #include <linux/nfs_mount.h>
  31. #include <linux/pagemap.h>
  32. #include <linux/pagevec.h>
  33. #include <linux/namei.h>
  34. #include <linux/mount.h>
  35. #include <linux/swap.h>
  36. #include <linux/sched.h>
  37. #include <linux/kmemleak.h>
  38. #include <linux/xattr.h>
  39. #include "delegation.h"
  40. #include "iostat.h"
  41. #include "internal.h"
  42. #include "fscache.h"
  43. /* #define NFS_DEBUG_VERBOSE 1 */
  44. static int nfs_opendir(struct inode *, struct file *);
  45. static int nfs_closedir(struct inode *, struct file *);
  46. static int nfs_readdir(struct file *, struct dir_context *);
  47. static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
  48. static loff_t nfs_llseek_dir(struct file *, loff_t, int);
  49. static void nfs_readdir_clear_array(struct page*);
  50. const struct file_operations nfs_dir_operations = {
  51. .llseek = nfs_llseek_dir,
  52. .read = generic_read_dir,
  53. .iterate = nfs_readdir,
  54. .open = nfs_opendir,
  55. .release = nfs_closedir,
  56. .fsync = nfs_fsync_dir,
  57. };
  58. const struct address_space_operations nfs_dir_aops = {
  59. .freepage = nfs_readdir_clear_array,
  60. };
  61. static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, struct rpc_cred *cred)
  62. {
  63. struct nfs_open_dir_context *ctx;
  64. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  65. if (ctx != NULL) {
  66. ctx->duped = 0;
  67. ctx->attr_gencount = NFS_I(dir)->attr_gencount;
  68. ctx->dir_cookie = 0;
  69. ctx->dup_cookie = 0;
  70. ctx->cred = get_rpccred(cred);
  71. return ctx;
  72. }
  73. return ERR_PTR(-ENOMEM);
  74. }
  75. static void put_nfs_open_dir_context(struct nfs_open_dir_context *ctx)
  76. {
  77. put_rpccred(ctx->cred);
  78. kfree(ctx);
  79. }
  80. /*
  81. * Open file
  82. */
  83. static int
  84. nfs_opendir(struct inode *inode, struct file *filp)
  85. {
  86. int res = 0;
  87. struct nfs_open_dir_context *ctx;
  88. struct rpc_cred *cred;
  89. dfprintk(FILE, "NFS: open dir(%s/%s)\n",
  90. filp->f_path.dentry->d_parent->d_name.name,
  91. filp->f_path.dentry->d_name.name);
  92. nfs_inc_stats(inode, NFSIOS_VFSOPEN);
  93. cred = rpc_lookup_cred();
  94. if (IS_ERR(cred))
  95. return PTR_ERR(cred);
  96. ctx = alloc_nfs_open_dir_context(inode, cred);
  97. if (IS_ERR(ctx)) {
  98. res = PTR_ERR(ctx);
  99. goto out;
  100. }
  101. filp->private_data = ctx;
  102. if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
  103. /* This is a mountpoint, so d_revalidate will never
  104. * have been called, so we need to refresh the
  105. * inode (for close-open consistency) ourselves.
  106. */
  107. __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  108. }
  109. out:
  110. put_rpccred(cred);
  111. return res;
  112. }
  113. static int
  114. nfs_closedir(struct inode *inode, struct file *filp)
  115. {
  116. put_nfs_open_dir_context(filp->private_data);
  117. return 0;
  118. }
  119. struct nfs_cache_array_entry {
  120. u64 cookie;
  121. u64 ino;
  122. struct qstr string;
  123. unsigned char d_type;
  124. };
  125. struct nfs_cache_array {
  126. int size;
  127. int eof_index;
  128. u64 last_cookie;
  129. struct nfs_cache_array_entry array[0];
  130. };
  131. typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
  132. typedef struct {
  133. struct file *file;
  134. struct page *page;
  135. struct dir_context *ctx;
  136. unsigned long page_index;
  137. u64 *dir_cookie;
  138. u64 last_cookie;
  139. loff_t current_index;
  140. decode_dirent_t decode;
  141. unsigned long timestamp;
  142. unsigned long gencount;
  143. unsigned int cache_entry_index;
  144. unsigned int plus:1;
  145. unsigned int eof:1;
  146. } nfs_readdir_descriptor_t;
  147. /*
  148. * The caller is responsible for calling nfs_readdir_release_array(page)
  149. */
  150. static
  151. struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
  152. {
  153. void *ptr;
  154. if (page == NULL)
  155. return ERR_PTR(-EIO);
  156. ptr = kmap(page);
  157. if (ptr == NULL)
  158. return ERR_PTR(-ENOMEM);
  159. return ptr;
  160. }
  161. static
  162. void nfs_readdir_release_array(struct page *page)
  163. {
  164. kunmap(page);
  165. }
  166. /*
  167. * we are freeing strings created by nfs_add_to_readdir_array()
  168. */
  169. static
  170. void nfs_readdir_clear_array(struct page *page)
  171. {
  172. struct nfs_cache_array *array;
  173. int i;
  174. array = kmap_atomic(page);
  175. for (i = 0; i < array->size; i++)
  176. kfree(array->array[i].string.name);
  177. kunmap_atomic(array);
  178. }
  179. /*
  180. * the caller is responsible for freeing qstr.name
  181. * when called by nfs_readdir_add_to_array, the strings will be freed in
  182. * nfs_clear_readdir_array()
  183. */
  184. static
  185. int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
  186. {
  187. string->len = len;
  188. string->name = kmemdup(name, len, GFP_KERNEL);
  189. if (string->name == NULL)
  190. return -ENOMEM;
  191. /*
  192. * Avoid a kmemleak false positive. The pointer to the name is stored
  193. * in a page cache page which kmemleak does not scan.
  194. */
  195. kmemleak_not_leak(string->name);
  196. string->hash = full_name_hash(name, len);
  197. return 0;
  198. }
  199. static
  200. int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
  201. {
  202. struct nfs_cache_array *array = nfs_readdir_get_array(page);
  203. struct nfs_cache_array_entry *cache_entry;
  204. int ret;
  205. if (IS_ERR(array))
  206. return PTR_ERR(array);
  207. cache_entry = &array->array[array->size];
  208. /* Check that this entry lies within the page bounds */
  209. ret = -ENOSPC;
  210. if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
  211. goto out;
  212. cache_entry->cookie = entry->prev_cookie;
  213. cache_entry->ino = entry->ino;
  214. cache_entry->d_type = entry->d_type;
  215. ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
  216. if (ret)
  217. goto out;
  218. array->last_cookie = entry->cookie;
  219. array->size++;
  220. if (entry->eof != 0)
  221. array->eof_index = array->size;
  222. out:
  223. nfs_readdir_release_array(page);
  224. return ret;
  225. }
  226. static
  227. int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
  228. {
  229. loff_t diff = desc->ctx->pos - desc->current_index;
  230. unsigned int index;
  231. if (diff < 0)
  232. goto out_eof;
  233. if (diff >= array->size) {
  234. if (array->eof_index >= 0)
  235. goto out_eof;
  236. return -EAGAIN;
  237. }
  238. index = (unsigned int)diff;
  239. *desc->dir_cookie = array->array[index].cookie;
  240. desc->cache_entry_index = index;
  241. return 0;
  242. out_eof:
  243. desc->eof = 1;
  244. return -EBADCOOKIE;
  245. }
  246. static
  247. int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
  248. {
  249. int i;
  250. loff_t new_pos;
  251. int status = -EAGAIN;
  252. for (i = 0; i < array->size; i++) {
  253. if (array->array[i].cookie == *desc->dir_cookie) {
  254. struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
  255. struct nfs_open_dir_context *ctx = desc->file->private_data;
  256. new_pos = desc->current_index + i;
  257. if (ctx->attr_gencount != nfsi->attr_gencount
  258. || (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))) {
  259. ctx->duped = 0;
  260. ctx->attr_gencount = nfsi->attr_gencount;
  261. } else if (new_pos < desc->ctx->pos) {
  262. if (ctx->duped > 0
  263. && ctx->dup_cookie == *desc->dir_cookie) {
  264. if (printk_ratelimit()) {
  265. pr_notice("NFS: directory %s/%s contains a readdir loop."
  266. "Please contact your server vendor. "
  267. "The file: %s has duplicate cookie %llu\n",
  268. desc->file->f_dentry->d_parent->d_name.name,
  269. desc->file->f_dentry->d_name.name,
  270. array->array[i].string.name,
  271. *desc->dir_cookie);
  272. }
  273. status = -ELOOP;
  274. goto out;
  275. }
  276. ctx->dup_cookie = *desc->dir_cookie;
  277. ctx->duped = -1;
  278. }
  279. desc->ctx->pos = new_pos;
  280. desc->cache_entry_index = i;
  281. return 0;
  282. }
  283. }
  284. if (array->eof_index >= 0) {
  285. status = -EBADCOOKIE;
  286. if (*desc->dir_cookie == array->last_cookie)
  287. desc->eof = 1;
  288. }
  289. out:
  290. return status;
  291. }
  292. static
  293. int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
  294. {
  295. struct nfs_cache_array *array;
  296. int status;
  297. array = nfs_readdir_get_array(desc->page);
  298. if (IS_ERR(array)) {
  299. status = PTR_ERR(array);
  300. goto out;
  301. }
  302. if (*desc->dir_cookie == 0)
  303. status = nfs_readdir_search_for_pos(array, desc);
  304. else
  305. status = nfs_readdir_search_for_cookie(array, desc);
  306. if (status == -EAGAIN) {
  307. desc->last_cookie = array->last_cookie;
  308. desc->current_index += array->size;
  309. desc->page_index++;
  310. }
  311. nfs_readdir_release_array(desc->page);
  312. out:
  313. return status;
  314. }
  315. /* Fill a page with xdr information before transferring to the cache page */
  316. static
  317. int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
  318. struct nfs_entry *entry, struct file *file, struct inode *inode)
  319. {
  320. struct nfs_open_dir_context *ctx = file->private_data;
  321. struct rpc_cred *cred = ctx->cred;
  322. unsigned long timestamp, gencount;
  323. int error;
  324. again:
  325. timestamp = jiffies;
  326. gencount = nfs_inc_attr_generation_counter();
  327. error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
  328. NFS_SERVER(inode)->dtsize, desc->plus);
  329. if (error < 0) {
  330. /* We requested READDIRPLUS, but the server doesn't grok it */
  331. if (error == -ENOTSUPP && desc->plus) {
  332. NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
  333. clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
  334. desc->plus = 0;
  335. goto again;
  336. }
  337. goto error;
  338. }
  339. desc->timestamp = timestamp;
  340. desc->gencount = gencount;
  341. error:
  342. return error;
  343. }
  344. static int xdr_decode(nfs_readdir_descriptor_t *desc,
  345. struct nfs_entry *entry, struct xdr_stream *xdr)
  346. {
  347. int error;
  348. error = desc->decode(xdr, entry, desc->plus);
  349. if (error)
  350. return error;
  351. entry->fattr->time_start = desc->timestamp;
  352. entry->fattr->gencount = desc->gencount;
  353. return 0;
  354. }
  355. static
  356. int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
  357. {
  358. if (dentry->d_inode == NULL)
  359. goto different;
  360. if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0)
  361. goto different;
  362. return 1;
  363. different:
  364. return 0;
  365. }
  366. static
  367. bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
  368. {
  369. if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
  370. return false;
  371. if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
  372. return true;
  373. if (ctx->pos == 0)
  374. return true;
  375. return false;
  376. }
  377. /*
  378. * This function is called by the lookup code to request the use of
  379. * readdirplus to accelerate any future lookups in the same
  380. * directory.
  381. */
  382. static
  383. void nfs_advise_use_readdirplus(struct inode *dir)
  384. {
  385. set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags);
  386. }
  387. static
  388. void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
  389. {
  390. struct qstr filename = QSTR_INIT(entry->name, entry->len);
  391. struct dentry *dentry;
  392. struct dentry *alias;
  393. struct inode *dir = parent->d_inode;
  394. struct inode *inode;
  395. int status;
  396. if (filename.name[0] == '.') {
  397. if (filename.len == 1)
  398. return;
  399. if (filename.len == 2 && filename.name[1] == '.')
  400. return;
  401. }
  402. filename.hash = full_name_hash(filename.name, filename.len);
  403. dentry = d_lookup(parent, &filename);
  404. if (dentry != NULL) {
  405. if (nfs_same_file(dentry, entry)) {
  406. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  407. status = nfs_refresh_inode(dentry->d_inode, entry->fattr);
  408. if (!status)
  409. nfs_setsecurity(dentry->d_inode, entry->fattr, entry->label);
  410. goto out;
  411. } else {
  412. if (d_invalidate(dentry) != 0)
  413. goto out;
  414. dput(dentry);
  415. }
  416. }
  417. dentry = d_alloc(parent, &filename);
  418. if (dentry == NULL)
  419. return;
  420. inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
  421. if (IS_ERR(inode))
  422. goto out;
  423. alias = d_materialise_unique(dentry, inode);
  424. if (IS_ERR(alias))
  425. goto out;
  426. else if (alias) {
  427. nfs_set_verifier(alias, nfs_save_change_attribute(dir));
  428. dput(alias);
  429. } else
  430. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  431. out:
  432. dput(dentry);
  433. }
  434. /* Perform conversion from xdr to cache array */
  435. static
  436. int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
  437. struct page **xdr_pages, struct page *page, unsigned int buflen)
  438. {
  439. struct xdr_stream stream;
  440. struct xdr_buf buf;
  441. struct page *scratch;
  442. struct nfs_cache_array *array;
  443. unsigned int count = 0;
  444. int status;
  445. scratch = alloc_page(GFP_KERNEL);
  446. if (scratch == NULL)
  447. return -ENOMEM;
  448. xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
  449. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  450. do {
  451. status = xdr_decode(desc, entry, &stream);
  452. if (status != 0) {
  453. if (status == -EAGAIN)
  454. status = 0;
  455. break;
  456. }
  457. count++;
  458. if (desc->plus != 0)
  459. nfs_prime_dcache(desc->file->f_path.dentry, entry);
  460. status = nfs_readdir_add_to_array(entry, page);
  461. if (status != 0)
  462. break;
  463. } while (!entry->eof);
  464. if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
  465. array = nfs_readdir_get_array(page);
  466. if (!IS_ERR(array)) {
  467. array->eof_index = array->size;
  468. status = 0;
  469. nfs_readdir_release_array(page);
  470. } else
  471. status = PTR_ERR(array);
  472. }
  473. put_page(scratch);
  474. return status;
  475. }
  476. static
  477. void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
  478. {
  479. unsigned int i;
  480. for (i = 0; i < npages; i++)
  481. put_page(pages[i]);
  482. }
  483. static
  484. void nfs_readdir_free_large_page(void *ptr, struct page **pages,
  485. unsigned int npages)
  486. {
  487. nfs_readdir_free_pagearray(pages, npages);
  488. }
  489. /*
  490. * nfs_readdir_large_page will allocate pages that must be freed with a call
  491. * to nfs_readdir_free_large_page
  492. */
  493. static
  494. int nfs_readdir_large_page(struct page **pages, unsigned int npages)
  495. {
  496. unsigned int i;
  497. for (i = 0; i < npages; i++) {
  498. struct page *page = alloc_page(GFP_KERNEL);
  499. if (page == NULL)
  500. goto out_freepages;
  501. pages[i] = page;
  502. }
  503. return 0;
  504. out_freepages:
  505. nfs_readdir_free_pagearray(pages, i);
  506. return -ENOMEM;
  507. }
  508. static
  509. int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
  510. {
  511. struct page *pages[NFS_MAX_READDIR_PAGES];
  512. void *pages_ptr = NULL;
  513. struct nfs_entry entry;
  514. struct file *file = desc->file;
  515. struct nfs_cache_array *array;
  516. int status = -ENOMEM;
  517. unsigned int array_size = ARRAY_SIZE(pages);
  518. entry.prev_cookie = 0;
  519. entry.cookie = desc->last_cookie;
  520. entry.eof = 0;
  521. entry.fh = nfs_alloc_fhandle();
  522. entry.fattr = nfs_alloc_fattr();
  523. entry.server = NFS_SERVER(inode);
  524. if (entry.fh == NULL || entry.fattr == NULL)
  525. goto out;
  526. entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
  527. if (IS_ERR(entry.label)) {
  528. status = PTR_ERR(entry.label);
  529. goto out;
  530. }
  531. array = nfs_readdir_get_array(page);
  532. if (IS_ERR(array)) {
  533. status = PTR_ERR(array);
  534. goto out_label_free;
  535. }
  536. memset(array, 0, sizeof(struct nfs_cache_array));
  537. array->eof_index = -1;
  538. status = nfs_readdir_large_page(pages, array_size);
  539. if (status < 0)
  540. goto out_release_array;
  541. do {
  542. unsigned int pglen;
  543. status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
  544. if (status < 0)
  545. break;
  546. pglen = status;
  547. status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
  548. if (status < 0) {
  549. if (status == -ENOSPC)
  550. status = 0;
  551. break;
  552. }
  553. } while (array->eof_index < 0);
  554. nfs_readdir_free_large_page(pages_ptr, pages, array_size);
  555. out_release_array:
  556. nfs_readdir_release_array(page);
  557. out_label_free:
  558. nfs4_label_free(entry.label);
  559. out:
  560. nfs_free_fattr(entry.fattr);
  561. nfs_free_fhandle(entry.fh);
  562. return status;
  563. }
  564. /*
  565. * Now we cache directories properly, by converting xdr information
  566. * to an array that can be used for lookups later. This results in
  567. * fewer cache pages, since we can store more information on each page.
  568. * We only need to convert from xdr once so future lookups are much simpler
  569. */
  570. static
  571. int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
  572. {
  573. struct inode *inode = file_inode(desc->file);
  574. int ret;
  575. ret = nfs_readdir_xdr_to_array(desc, page, inode);
  576. if (ret < 0)
  577. goto error;
  578. SetPageUptodate(page);
  579. if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
  580. /* Should never happen */
  581. nfs_zap_mapping(inode, inode->i_mapping);
  582. }
  583. unlock_page(page);
  584. return 0;
  585. error:
  586. unlock_page(page);
  587. return ret;
  588. }
  589. static
  590. void cache_page_release(nfs_readdir_descriptor_t *desc)
  591. {
  592. if (!desc->page->mapping)
  593. nfs_readdir_clear_array(desc->page);
  594. page_cache_release(desc->page);
  595. desc->page = NULL;
  596. }
  597. static
  598. struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
  599. {
  600. return read_cache_page(file_inode(desc->file)->i_mapping,
  601. desc->page_index, (filler_t *)nfs_readdir_filler, desc);
  602. }
  603. /*
  604. * Returns 0 if desc->dir_cookie was found on page desc->page_index
  605. */
  606. static
  607. int find_cache_page(nfs_readdir_descriptor_t *desc)
  608. {
  609. int res;
  610. desc->page = get_cache_page(desc);
  611. if (IS_ERR(desc->page))
  612. return PTR_ERR(desc->page);
  613. res = nfs_readdir_search_array(desc);
  614. if (res != 0)
  615. cache_page_release(desc);
  616. return res;
  617. }
  618. /* Search for desc->dir_cookie from the beginning of the page cache */
  619. static inline
  620. int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
  621. {
  622. int res;
  623. if (desc->page_index == 0) {
  624. desc->current_index = 0;
  625. desc->last_cookie = 0;
  626. }
  627. do {
  628. res = find_cache_page(desc);
  629. } while (res == -EAGAIN);
  630. return res;
  631. }
  632. /*
  633. * Once we've found the start of the dirent within a page: fill 'er up...
  634. */
  635. static
  636. int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
  637. {
  638. struct file *file = desc->file;
  639. int i = 0;
  640. int res = 0;
  641. struct nfs_cache_array *array = NULL;
  642. struct nfs_open_dir_context *ctx = file->private_data;
  643. array = nfs_readdir_get_array(desc->page);
  644. if (IS_ERR(array)) {
  645. res = PTR_ERR(array);
  646. goto out;
  647. }
  648. for (i = desc->cache_entry_index; i < array->size; i++) {
  649. struct nfs_cache_array_entry *ent;
  650. ent = &array->array[i];
  651. if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
  652. nfs_compat_user_ino64(ent->ino), ent->d_type)) {
  653. desc->eof = 1;
  654. break;
  655. }
  656. desc->ctx->pos++;
  657. if (i < (array->size-1))
  658. *desc->dir_cookie = array->array[i+1].cookie;
  659. else
  660. *desc->dir_cookie = array->last_cookie;
  661. if (ctx->duped != 0)
  662. ctx->duped = 1;
  663. }
  664. if (array->eof_index >= 0)
  665. desc->eof = 1;
  666. nfs_readdir_release_array(desc->page);
  667. out:
  668. cache_page_release(desc);
  669. dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
  670. (unsigned long long)*desc->dir_cookie, res);
  671. return res;
  672. }
  673. /*
  674. * If we cannot find a cookie in our cache, we suspect that this is
  675. * because it points to a deleted file, so we ask the server to return
  676. * whatever it thinks is the next entry. We then feed this to filldir.
  677. * If all goes well, we should then be able to find our way round the
  678. * cache on the next call to readdir_search_pagecache();
  679. *
  680. * NOTE: we cannot add the anonymous page to the pagecache because
  681. * the data it contains might not be page aligned. Besides,
  682. * we should already have a complete representation of the
  683. * directory in the page cache by the time we get here.
  684. */
  685. static inline
  686. int uncached_readdir(nfs_readdir_descriptor_t *desc)
  687. {
  688. struct page *page = NULL;
  689. int status;
  690. struct inode *inode = file_inode(desc->file);
  691. struct nfs_open_dir_context *ctx = desc->file->private_data;
  692. dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
  693. (unsigned long long)*desc->dir_cookie);
  694. page = alloc_page(GFP_HIGHUSER);
  695. if (!page) {
  696. status = -ENOMEM;
  697. goto out;
  698. }
  699. desc->page_index = 0;
  700. desc->last_cookie = *desc->dir_cookie;
  701. desc->page = page;
  702. ctx->duped = 0;
  703. status = nfs_readdir_xdr_to_array(desc, page, inode);
  704. if (status < 0)
  705. goto out_release;
  706. status = nfs_do_filldir(desc);
  707. out:
  708. dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
  709. __func__, status);
  710. return status;
  711. out_release:
  712. cache_page_release(desc);
  713. goto out;
  714. }
  715. /* The file offset position represents the dirent entry number. A
  716. last cookie cache takes care of the common case of reading the
  717. whole directory.
  718. */
  719. static int nfs_readdir(struct file *file, struct dir_context *ctx)
  720. {
  721. struct dentry *dentry = file->f_path.dentry;
  722. struct inode *inode = dentry->d_inode;
  723. nfs_readdir_descriptor_t my_desc,
  724. *desc = &my_desc;
  725. struct nfs_open_dir_context *dir_ctx = file->private_data;
  726. int res = 0;
  727. dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
  728. dentry->d_parent->d_name.name, dentry->d_name.name,
  729. (long long)ctx->pos);
  730. nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
  731. /*
  732. * ctx->pos points to the dirent entry number.
  733. * *desc->dir_cookie has the cookie for the next entry. We have
  734. * to either find the entry with the appropriate number or
  735. * revalidate the cookie.
  736. */
  737. memset(desc, 0, sizeof(*desc));
  738. desc->file = file;
  739. desc->ctx = ctx;
  740. desc->dir_cookie = &dir_ctx->dir_cookie;
  741. desc->decode = NFS_PROTO(inode)->decode_dirent;
  742. desc->plus = nfs_use_readdirplus(inode, ctx) ? 1 : 0;
  743. nfs_block_sillyrename(dentry);
  744. if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
  745. res = nfs_revalidate_mapping(inode, file->f_mapping);
  746. if (res < 0)
  747. goto out;
  748. do {
  749. res = readdir_search_pagecache(desc);
  750. if (res == -EBADCOOKIE) {
  751. res = 0;
  752. /* This means either end of directory */
  753. if (*desc->dir_cookie && desc->eof == 0) {
  754. /* Or that the server has 'lost' a cookie */
  755. res = uncached_readdir(desc);
  756. if (res == 0)
  757. continue;
  758. }
  759. break;
  760. }
  761. if (res == -ETOOSMALL && desc->plus) {
  762. clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
  763. nfs_zap_caches(inode);
  764. desc->page_index = 0;
  765. desc->plus = 0;
  766. desc->eof = 0;
  767. continue;
  768. }
  769. if (res < 0)
  770. break;
  771. res = nfs_do_filldir(desc);
  772. if (res < 0)
  773. break;
  774. } while (!desc->eof);
  775. out:
  776. nfs_unblock_sillyrename(dentry);
  777. if (res > 0)
  778. res = 0;
  779. dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
  780. dentry->d_parent->d_name.name, dentry->d_name.name,
  781. res);
  782. return res;
  783. }
  784. static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
  785. {
  786. struct dentry *dentry = filp->f_path.dentry;
  787. struct inode *inode = dentry->d_inode;
  788. struct nfs_open_dir_context *dir_ctx = filp->private_data;
  789. dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
  790. dentry->d_parent->d_name.name,
  791. dentry->d_name.name,
  792. offset, whence);
  793. mutex_lock(&inode->i_mutex);
  794. switch (whence) {
  795. case 1:
  796. offset += filp->f_pos;
  797. case 0:
  798. if (offset >= 0)
  799. break;
  800. default:
  801. offset = -EINVAL;
  802. goto out;
  803. }
  804. if (offset != filp->f_pos) {
  805. filp->f_pos = offset;
  806. dir_ctx->dir_cookie = 0;
  807. dir_ctx->duped = 0;
  808. }
  809. out:
  810. mutex_unlock(&inode->i_mutex);
  811. return offset;
  812. }
  813. /*
  814. * All directory operations under NFS are synchronous, so fsync()
  815. * is a dummy operation.
  816. */
  817. static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
  818. int datasync)
  819. {
  820. struct dentry *dentry = filp->f_path.dentry;
  821. struct inode *inode = dentry->d_inode;
  822. dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
  823. dentry->d_parent->d_name.name, dentry->d_name.name,
  824. datasync);
  825. mutex_lock(&inode->i_mutex);
  826. nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
  827. mutex_unlock(&inode->i_mutex);
  828. return 0;
  829. }
  830. /**
  831. * nfs_force_lookup_revalidate - Mark the directory as having changed
  832. * @dir - pointer to directory inode
  833. *
  834. * This forces the revalidation code in nfs_lookup_revalidate() to do a
  835. * full lookup on all child dentries of 'dir' whenever a change occurs
  836. * on the server that might have invalidated our dcache.
  837. *
  838. * The caller should be holding dir->i_lock
  839. */
  840. void nfs_force_lookup_revalidate(struct inode *dir)
  841. {
  842. NFS_I(dir)->cache_change_attribute++;
  843. }
  844. EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
  845. /*
  846. * A check for whether or not the parent directory has changed.
  847. * In the case it has, we assume that the dentries are untrustworthy
  848. * and may need to be looked up again.
  849. */
  850. static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
  851. {
  852. if (IS_ROOT(dentry))
  853. return 1;
  854. if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
  855. return 0;
  856. if (!nfs_verify_change_attribute(dir, dentry->d_time))
  857. return 0;
  858. /* Revalidate nfsi->cache_change_attribute before we declare a match */
  859. if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
  860. return 0;
  861. if (!nfs_verify_change_attribute(dir, dentry->d_time))
  862. return 0;
  863. return 1;
  864. }
  865. /*
  866. * Use intent information to check whether or not we're going to do
  867. * an O_EXCL create using this path component.
  868. */
  869. static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
  870. {
  871. if (NFS_PROTO(dir)->version == 2)
  872. return 0;
  873. return flags & LOOKUP_EXCL;
  874. }
  875. /*
  876. * Inode and filehandle revalidation for lookups.
  877. *
  878. * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
  879. * or if the intent information indicates that we're about to open this
  880. * particular file and the "nocto" mount flag is not set.
  881. *
  882. */
  883. static
  884. int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
  885. {
  886. struct nfs_server *server = NFS_SERVER(inode);
  887. int ret;
  888. if (IS_AUTOMOUNT(inode))
  889. return 0;
  890. /* VFS wants an on-the-wire revalidation */
  891. if (flags & LOOKUP_REVAL)
  892. goto out_force;
  893. /* This is an open(2) */
  894. if ((flags & LOOKUP_OPEN) && !(server->flags & NFS_MOUNT_NOCTO) &&
  895. (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
  896. goto out_force;
  897. out:
  898. return (inode->i_nlink == 0) ? -ENOENT : 0;
  899. out_force:
  900. ret = __nfs_revalidate_inode(server, inode);
  901. if (ret != 0)
  902. return ret;
  903. goto out;
  904. }
  905. /*
  906. * We judge how long we want to trust negative
  907. * dentries by looking at the parent inode mtime.
  908. *
  909. * If parent mtime has changed, we revalidate, else we wait for a
  910. * period corresponding to the parent's attribute cache timeout value.
  911. */
  912. static inline
  913. int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
  914. unsigned int flags)
  915. {
  916. /* Don't revalidate a negative dentry if we're creating a new file */
  917. if (flags & LOOKUP_CREATE)
  918. return 0;
  919. if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
  920. return 1;
  921. return !nfs_check_verifier(dir, dentry);
  922. }
  923. /*
  924. * This is called every time the dcache has a lookup hit,
  925. * and we should check whether we can really trust that
  926. * lookup.
  927. *
  928. * NOTE! The hit can be a negative hit too, don't assume
  929. * we have an inode!
  930. *
  931. * If the parent directory is seen to have changed, we throw out the
  932. * cached dentry and do a new lookup.
  933. */
  934. static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
  935. {
  936. struct inode *dir;
  937. struct inode *inode;
  938. struct dentry *parent;
  939. struct nfs_fh *fhandle = NULL;
  940. struct nfs_fattr *fattr = NULL;
  941. struct nfs4_label *label = NULL;
  942. int error;
  943. if (flags & LOOKUP_RCU)
  944. return -ECHILD;
  945. parent = dget_parent(dentry);
  946. dir = parent->d_inode;
  947. nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
  948. inode = dentry->d_inode;
  949. if (!inode) {
  950. if (nfs_neg_need_reval(dir, dentry, flags))
  951. goto out_bad;
  952. goto out_valid_noent;
  953. }
  954. if (is_bad_inode(inode)) {
  955. dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
  956. __func__, dentry->d_parent->d_name.name,
  957. dentry->d_name.name);
  958. goto out_bad;
  959. }
  960. if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
  961. goto out_set_verifier;
  962. /* Force a full look up iff the parent directory has changed */
  963. if (!nfs_is_exclusive_create(dir, flags) && nfs_check_verifier(dir, dentry)) {
  964. if (nfs_lookup_verify_inode(inode, flags))
  965. goto out_zap_parent;
  966. goto out_valid;
  967. }
  968. if (NFS_STALE(inode))
  969. goto out_bad;
  970. error = -ENOMEM;
  971. fhandle = nfs_alloc_fhandle();
  972. fattr = nfs_alloc_fattr();
  973. if (fhandle == NULL || fattr == NULL)
  974. goto out_error;
  975. label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
  976. if (IS_ERR(label))
  977. goto out_error;
  978. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
  979. if (error)
  980. goto out_bad;
  981. if (nfs_compare_fh(NFS_FH(inode), fhandle))
  982. goto out_bad;
  983. if ((error = nfs_refresh_inode(inode, fattr)) != 0)
  984. goto out_bad;
  985. nfs_setsecurity(inode, fattr, label);
  986. nfs_free_fattr(fattr);
  987. nfs_free_fhandle(fhandle);
  988. nfs4_label_free(label);
  989. out_set_verifier:
  990. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  991. out_valid:
  992. /* Success: notify readdir to use READDIRPLUS */
  993. nfs_advise_use_readdirplus(dir);
  994. out_valid_noent:
  995. dput(parent);
  996. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
  997. __func__, dentry->d_parent->d_name.name,
  998. dentry->d_name.name);
  999. return 1;
  1000. out_zap_parent:
  1001. nfs_zap_caches(dir);
  1002. out_bad:
  1003. nfs_free_fattr(fattr);
  1004. nfs_free_fhandle(fhandle);
  1005. nfs4_label_free(label);
  1006. nfs_mark_for_revalidate(dir);
  1007. if (inode && S_ISDIR(inode->i_mode)) {
  1008. /* Purge readdir caches. */
  1009. nfs_zap_caches(inode);
  1010. /* If we have submounts, don't unhash ! */
  1011. if (have_submounts(dentry))
  1012. goto out_valid;
  1013. if (dentry->d_flags & DCACHE_DISCONNECTED)
  1014. goto out_valid;
  1015. shrink_dcache_parent(dentry);
  1016. }
  1017. d_drop(dentry);
  1018. dput(parent);
  1019. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
  1020. __func__, dentry->d_parent->d_name.name,
  1021. dentry->d_name.name);
  1022. return 0;
  1023. out_error:
  1024. nfs_free_fattr(fattr);
  1025. nfs_free_fhandle(fhandle);
  1026. nfs4_label_free(label);
  1027. dput(parent);
  1028. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
  1029. __func__, dentry->d_parent->d_name.name,
  1030. dentry->d_name.name, error);
  1031. return error;
  1032. }
  1033. /*
  1034. * A weaker form of d_revalidate for revalidating just the dentry->d_inode
  1035. * when we don't really care about the dentry name. This is called when a
  1036. * pathwalk ends on a dentry that was not found via a normal lookup in the
  1037. * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
  1038. *
  1039. * In this situation, we just want to verify that the inode itself is OK
  1040. * since the dentry might have changed on the server.
  1041. */
  1042. static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
  1043. {
  1044. int error;
  1045. struct inode *inode = dentry->d_inode;
  1046. /*
  1047. * I believe we can only get a negative dentry here in the case of a
  1048. * procfs-style symlink. Just assume it's correct for now, but we may
  1049. * eventually need to do something more here.
  1050. */
  1051. if (!inode) {
  1052. dfprintk(LOOKUPCACHE, "%s: %s/%s has negative inode\n",
  1053. __func__, dentry->d_parent->d_name.name,
  1054. dentry->d_name.name);
  1055. return 1;
  1056. }
  1057. if (is_bad_inode(inode)) {
  1058. dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
  1059. __func__, dentry->d_parent->d_name.name,
  1060. dentry->d_name.name);
  1061. return 0;
  1062. }
  1063. error = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  1064. dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
  1065. __func__, inode->i_ino, error ? "invalid" : "valid");
  1066. return !error;
  1067. }
  1068. /*
  1069. * This is called from dput() when d_count is going to 0.
  1070. */
  1071. static int nfs_dentry_delete(const struct dentry *dentry)
  1072. {
  1073. dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
  1074. dentry->d_parent->d_name.name, dentry->d_name.name,
  1075. dentry->d_flags);
  1076. /* Unhash any dentry with a stale inode */
  1077. if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
  1078. return 1;
  1079. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1080. /* Unhash it, so that ->d_iput() would be called */
  1081. return 1;
  1082. }
  1083. if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
  1084. /* Unhash it, so that ancestors of killed async unlink
  1085. * files will be cleaned up during umount */
  1086. return 1;
  1087. }
  1088. return 0;
  1089. }
  1090. /* Ensure that we revalidate inode->i_nlink */
  1091. static void nfs_drop_nlink(struct inode *inode)
  1092. {
  1093. spin_lock(&inode->i_lock);
  1094. /* drop the inode if we're reasonably sure this is the last link */
  1095. if (inode->i_nlink == 1)
  1096. clear_nlink(inode);
  1097. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
  1098. spin_unlock(&inode->i_lock);
  1099. }
  1100. /*
  1101. * Called when the dentry loses inode.
  1102. * We use it to clean up silly-renamed files.
  1103. */
  1104. static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
  1105. {
  1106. if (S_ISDIR(inode->i_mode))
  1107. /* drop any readdir cache as it could easily be old */
  1108. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
  1109. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1110. nfs_complete_unlink(dentry, inode);
  1111. nfs_drop_nlink(inode);
  1112. }
  1113. iput(inode);
  1114. }
  1115. static void nfs_d_release(struct dentry *dentry)
  1116. {
  1117. /* free cached devname value, if it survived that far */
  1118. if (unlikely(dentry->d_fsdata)) {
  1119. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1120. WARN_ON(1);
  1121. else
  1122. kfree(dentry->d_fsdata);
  1123. }
  1124. }
  1125. const struct dentry_operations nfs_dentry_operations = {
  1126. .d_revalidate = nfs_lookup_revalidate,
  1127. .d_weak_revalidate = nfs_weak_revalidate,
  1128. .d_delete = nfs_dentry_delete,
  1129. .d_iput = nfs_dentry_iput,
  1130. .d_automount = nfs_d_automount,
  1131. .d_release = nfs_d_release,
  1132. };
  1133. EXPORT_SYMBOL_GPL(nfs_dentry_operations);
  1134. struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
  1135. {
  1136. struct dentry *res;
  1137. struct dentry *parent;
  1138. struct inode *inode = NULL;
  1139. struct nfs_fh *fhandle = NULL;
  1140. struct nfs_fattr *fattr = NULL;
  1141. struct nfs4_label *label = NULL;
  1142. int error;
  1143. dfprintk(VFS, "NFS: lookup(%s/%s)\n",
  1144. dentry->d_parent->d_name.name, dentry->d_name.name);
  1145. nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
  1146. res = ERR_PTR(-ENAMETOOLONG);
  1147. if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
  1148. goto out;
  1149. /*
  1150. * If we're doing an exclusive create, optimize away the lookup
  1151. * but don't hash the dentry.
  1152. */
  1153. if (nfs_is_exclusive_create(dir, flags)) {
  1154. d_instantiate(dentry, NULL);
  1155. res = NULL;
  1156. goto out;
  1157. }
  1158. res = ERR_PTR(-ENOMEM);
  1159. fhandle = nfs_alloc_fhandle();
  1160. fattr = nfs_alloc_fattr();
  1161. if (fhandle == NULL || fattr == NULL)
  1162. goto out;
  1163. label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
  1164. if (IS_ERR(label))
  1165. goto out;
  1166. parent = dentry->d_parent;
  1167. /* Protect against concurrent sillydeletes */
  1168. nfs_block_sillyrename(parent);
  1169. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
  1170. if (error == -ENOENT)
  1171. goto no_entry;
  1172. if (error < 0) {
  1173. res = ERR_PTR(error);
  1174. goto out_unblock_sillyrename;
  1175. }
  1176. inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
  1177. res = ERR_CAST(inode);
  1178. if (IS_ERR(res))
  1179. goto out_unblock_sillyrename;
  1180. /* Success: notify readdir to use READDIRPLUS */
  1181. nfs_advise_use_readdirplus(dir);
  1182. no_entry:
  1183. res = d_materialise_unique(dentry, inode);
  1184. if (res != NULL) {
  1185. if (IS_ERR(res))
  1186. goto out_unblock_sillyrename;
  1187. dentry = res;
  1188. }
  1189. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1190. out_unblock_sillyrename:
  1191. nfs_unblock_sillyrename(parent);
  1192. nfs4_label_free(label);
  1193. out:
  1194. nfs_free_fattr(fattr);
  1195. nfs_free_fhandle(fhandle);
  1196. return res;
  1197. }
  1198. EXPORT_SYMBOL_GPL(nfs_lookup);
  1199. #if IS_ENABLED(CONFIG_NFS_V4)
  1200. static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
  1201. const struct dentry_operations nfs4_dentry_operations = {
  1202. .d_revalidate = nfs4_lookup_revalidate,
  1203. .d_delete = nfs_dentry_delete,
  1204. .d_iput = nfs_dentry_iput,
  1205. .d_automount = nfs_d_automount,
  1206. .d_release = nfs_d_release,
  1207. };
  1208. EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
  1209. static fmode_t flags_to_mode(int flags)
  1210. {
  1211. fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
  1212. if ((flags & O_ACCMODE) != O_WRONLY)
  1213. res |= FMODE_READ;
  1214. if ((flags & O_ACCMODE) != O_RDONLY)
  1215. res |= FMODE_WRITE;
  1216. return res;
  1217. }
  1218. static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags)
  1219. {
  1220. return alloc_nfs_open_context(dentry, flags_to_mode(open_flags));
  1221. }
  1222. static int do_open(struct inode *inode, struct file *filp)
  1223. {
  1224. nfs_fscache_set_inode_cookie(inode, filp);
  1225. return 0;
  1226. }
  1227. static int nfs_finish_open(struct nfs_open_context *ctx,
  1228. struct dentry *dentry,
  1229. struct file *file, unsigned open_flags,
  1230. int *opened)
  1231. {
  1232. int err;
  1233. err = finish_open(file, dentry, do_open, opened);
  1234. if (err)
  1235. goto out;
  1236. nfs_file_set_open_context(file, ctx);
  1237. out:
  1238. put_nfs_open_context(ctx);
  1239. return err;
  1240. }
  1241. int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
  1242. struct file *file, unsigned open_flags,
  1243. umode_t mode, int *opened)
  1244. {
  1245. struct nfs_open_context *ctx;
  1246. struct dentry *res;
  1247. struct iattr attr = { .ia_valid = ATTR_OPEN };
  1248. struct inode *inode;
  1249. int err;
  1250. /* Expect a negative dentry */
  1251. BUG_ON(dentry->d_inode);
  1252. dfprintk(VFS, "NFS: atomic_open(%s/%ld), %s\n",
  1253. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1254. err = nfs_check_flags(open_flags);
  1255. if (err)
  1256. return err;
  1257. /* NFS only supports OPEN on regular files */
  1258. if ((open_flags & O_DIRECTORY)) {
  1259. if (!d_unhashed(dentry)) {
  1260. /*
  1261. * Hashed negative dentry with O_DIRECTORY: dentry was
  1262. * revalidated and is fine, no need to perform lookup
  1263. * again
  1264. */
  1265. return -ENOENT;
  1266. }
  1267. goto no_open;
  1268. }
  1269. if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
  1270. return -ENAMETOOLONG;
  1271. if (open_flags & O_CREAT) {
  1272. attr.ia_valid |= ATTR_MODE;
  1273. attr.ia_mode = mode & ~current_umask();
  1274. }
  1275. if (open_flags & O_TRUNC) {
  1276. attr.ia_valid |= ATTR_SIZE;
  1277. attr.ia_size = 0;
  1278. }
  1279. ctx = create_nfs_open_context(dentry, open_flags);
  1280. err = PTR_ERR(ctx);
  1281. if (IS_ERR(ctx))
  1282. goto out;
  1283. nfs_block_sillyrename(dentry->d_parent);
  1284. inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
  1285. nfs_unblock_sillyrename(dentry->d_parent);
  1286. if (IS_ERR(inode)) {
  1287. put_nfs_open_context(ctx);
  1288. err = PTR_ERR(inode);
  1289. switch (err) {
  1290. case -ENOENT:
  1291. d_drop(dentry);
  1292. d_add(dentry, NULL);
  1293. break;
  1294. case -EISDIR:
  1295. case -ENOTDIR:
  1296. goto no_open;
  1297. case -ELOOP:
  1298. if (!(open_flags & O_NOFOLLOW))
  1299. goto no_open;
  1300. break;
  1301. /* case -EINVAL: */
  1302. default:
  1303. break;
  1304. }
  1305. goto out;
  1306. }
  1307. err = nfs_finish_open(ctx, ctx->dentry, file, open_flags, opened);
  1308. out:
  1309. return err;
  1310. no_open:
  1311. res = nfs_lookup(dir, dentry, 0);
  1312. err = PTR_ERR(res);
  1313. if (IS_ERR(res))
  1314. goto out;
  1315. return finish_no_open(file, res);
  1316. }
  1317. EXPORT_SYMBOL_GPL(nfs_atomic_open);
  1318. static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
  1319. {
  1320. struct dentry *parent = NULL;
  1321. struct inode *inode;
  1322. struct inode *dir;
  1323. int ret = 0;
  1324. if (flags & LOOKUP_RCU)
  1325. return -ECHILD;
  1326. if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
  1327. goto no_open;
  1328. if (d_mountpoint(dentry))
  1329. goto no_open;
  1330. if (NFS_SB(dentry->d_sb)->caps & NFS_CAP_ATOMIC_OPEN_V1)
  1331. goto no_open;
  1332. inode = dentry->d_inode;
  1333. parent = dget_parent(dentry);
  1334. dir = parent->d_inode;
  1335. /* We can't create new files in nfs_open_revalidate(), so we
  1336. * optimize away revalidation of negative dentries.
  1337. */
  1338. if (inode == NULL) {
  1339. if (!nfs_neg_need_reval(dir, dentry, flags))
  1340. ret = 1;
  1341. goto out;
  1342. }
  1343. /* NFS only supports OPEN on regular files */
  1344. if (!S_ISREG(inode->i_mode))
  1345. goto no_open_dput;
  1346. /* We cannot do exclusive creation on a positive dentry */
  1347. if (flags & LOOKUP_EXCL)
  1348. goto no_open_dput;
  1349. /* Let f_op->open() actually open (and revalidate) the file */
  1350. ret = 1;
  1351. out:
  1352. dput(parent);
  1353. return ret;
  1354. no_open_dput:
  1355. dput(parent);
  1356. no_open:
  1357. return nfs_lookup_revalidate(dentry, flags);
  1358. }
  1359. #endif /* CONFIG_NFSV4 */
  1360. /*
  1361. * Code common to create, mkdir, and mknod.
  1362. */
  1363. int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
  1364. struct nfs_fattr *fattr,
  1365. struct nfs4_label *label)
  1366. {
  1367. struct dentry *parent = dget_parent(dentry);
  1368. struct inode *dir = parent->d_inode;
  1369. struct inode *inode;
  1370. int error = -EACCES;
  1371. d_drop(dentry);
  1372. /* We may have been initialized further down */
  1373. if (dentry->d_inode)
  1374. goto out;
  1375. if (fhandle->size == 0) {
  1376. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL);
  1377. if (error)
  1378. goto out_error;
  1379. }
  1380. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1381. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  1382. struct nfs_server *server = NFS_SB(dentry->d_sb);
  1383. error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr, NULL);
  1384. if (error < 0)
  1385. goto out_error;
  1386. }
  1387. inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
  1388. error = PTR_ERR(inode);
  1389. if (IS_ERR(inode))
  1390. goto out_error;
  1391. d_add(dentry, inode);
  1392. out:
  1393. dput(parent);
  1394. return 0;
  1395. out_error:
  1396. nfs_mark_for_revalidate(dir);
  1397. dput(parent);
  1398. return error;
  1399. }
  1400. EXPORT_SYMBOL_GPL(nfs_instantiate);
  1401. /*
  1402. * Following a failed create operation, we drop the dentry rather
  1403. * than retain a negative dentry. This avoids a problem in the event
  1404. * that the operation succeeded on the server, but an error in the
  1405. * reply path made it appear to have failed.
  1406. */
  1407. int nfs_create(struct inode *dir, struct dentry *dentry,
  1408. umode_t mode, bool excl)
  1409. {
  1410. struct iattr attr;
  1411. int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
  1412. int error;
  1413. dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
  1414. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1415. attr.ia_mode = mode;
  1416. attr.ia_valid = ATTR_MODE;
  1417. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
  1418. if (error != 0)
  1419. goto out_err;
  1420. return 0;
  1421. out_err:
  1422. d_drop(dentry);
  1423. return error;
  1424. }
  1425. EXPORT_SYMBOL_GPL(nfs_create);
  1426. /*
  1427. * See comments for nfs_proc_create regarding failed operations.
  1428. */
  1429. int
  1430. nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
  1431. {
  1432. struct iattr attr;
  1433. int status;
  1434. dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
  1435. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1436. if (!new_valid_dev(rdev))
  1437. return -EINVAL;
  1438. attr.ia_mode = mode;
  1439. attr.ia_valid = ATTR_MODE;
  1440. status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
  1441. if (status != 0)
  1442. goto out_err;
  1443. return 0;
  1444. out_err:
  1445. d_drop(dentry);
  1446. return status;
  1447. }
  1448. EXPORT_SYMBOL_GPL(nfs_mknod);
  1449. /*
  1450. * See comments for nfs_proc_create regarding failed operations.
  1451. */
  1452. int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  1453. {
  1454. struct iattr attr;
  1455. int error;
  1456. dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
  1457. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1458. attr.ia_valid = ATTR_MODE;
  1459. attr.ia_mode = mode | S_IFDIR;
  1460. error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
  1461. if (error != 0)
  1462. goto out_err;
  1463. return 0;
  1464. out_err:
  1465. d_drop(dentry);
  1466. return error;
  1467. }
  1468. EXPORT_SYMBOL_GPL(nfs_mkdir);
  1469. static void nfs_dentry_handle_enoent(struct dentry *dentry)
  1470. {
  1471. if (dentry->d_inode != NULL && !d_unhashed(dentry))
  1472. d_delete(dentry);
  1473. }
  1474. int nfs_rmdir(struct inode *dir, struct dentry *dentry)
  1475. {
  1476. int error;
  1477. dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
  1478. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1479. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  1480. /* Ensure the VFS deletes this inode */
  1481. if (error == 0 && dentry->d_inode != NULL)
  1482. clear_nlink(dentry->d_inode);
  1483. else if (error == -ENOENT)
  1484. nfs_dentry_handle_enoent(dentry);
  1485. return error;
  1486. }
  1487. EXPORT_SYMBOL_GPL(nfs_rmdir);
  1488. /*
  1489. * Remove a file after making sure there are no pending writes,
  1490. * and after checking that the file has only one user.
  1491. *
  1492. * We invalidate the attribute cache and free the inode prior to the operation
  1493. * to avoid possible races if the server reuses the inode.
  1494. */
  1495. static int nfs_safe_remove(struct dentry *dentry)
  1496. {
  1497. struct inode *dir = dentry->d_parent->d_inode;
  1498. struct inode *inode = dentry->d_inode;
  1499. int error = -EBUSY;
  1500. dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
  1501. dentry->d_parent->d_name.name, dentry->d_name.name);
  1502. /* If the dentry was sillyrenamed, we simply call d_delete() */
  1503. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1504. error = 0;
  1505. goto out;
  1506. }
  1507. if (inode != NULL) {
  1508. NFS_PROTO(inode)->return_delegation(inode);
  1509. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1510. if (error == 0)
  1511. nfs_drop_nlink(inode);
  1512. } else
  1513. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1514. if (error == -ENOENT)
  1515. nfs_dentry_handle_enoent(dentry);
  1516. out:
  1517. return error;
  1518. }
  1519. /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
  1520. * belongs to an active ".nfs..." file and we return -EBUSY.
  1521. *
  1522. * If sillyrename() returns 0, we do nothing, otherwise we unlink.
  1523. */
  1524. int nfs_unlink(struct inode *dir, struct dentry *dentry)
  1525. {
  1526. int error;
  1527. int need_rehash = 0;
  1528. dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
  1529. dir->i_ino, dentry->d_name.name);
  1530. spin_lock(&dentry->d_lock);
  1531. if (d_count(dentry) > 1) {
  1532. spin_unlock(&dentry->d_lock);
  1533. /* Start asynchronous writeout of the inode */
  1534. write_inode_now(dentry->d_inode, 0);
  1535. error = nfs_sillyrename(dir, dentry);
  1536. return error;
  1537. }
  1538. if (!d_unhashed(dentry)) {
  1539. __d_drop(dentry);
  1540. need_rehash = 1;
  1541. }
  1542. spin_unlock(&dentry->d_lock);
  1543. error = nfs_safe_remove(dentry);
  1544. if (!error || error == -ENOENT) {
  1545. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1546. } else if (need_rehash)
  1547. d_rehash(dentry);
  1548. return error;
  1549. }
  1550. EXPORT_SYMBOL_GPL(nfs_unlink);
  1551. /*
  1552. * To create a symbolic link, most file systems instantiate a new inode,
  1553. * add a page to it containing the path, then write it out to the disk
  1554. * using prepare_write/commit_write.
  1555. *
  1556. * Unfortunately the NFS client can't create the in-core inode first
  1557. * because it needs a file handle to create an in-core inode (see
  1558. * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
  1559. * symlink request has completed on the server.
  1560. *
  1561. * So instead we allocate a raw page, copy the symname into it, then do
  1562. * the SYMLINK request with the page as the buffer. If it succeeds, we
  1563. * now have a new file handle and can instantiate an in-core NFS inode
  1564. * and move the raw page into its mapping.
  1565. */
  1566. int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1567. {
  1568. struct page *page;
  1569. char *kaddr;
  1570. struct iattr attr;
  1571. unsigned int pathlen = strlen(symname);
  1572. int error;
  1573. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
  1574. dir->i_ino, dentry->d_name.name, symname);
  1575. if (pathlen > PAGE_SIZE)
  1576. return -ENAMETOOLONG;
  1577. attr.ia_mode = S_IFLNK | S_IRWXUGO;
  1578. attr.ia_valid = ATTR_MODE;
  1579. page = alloc_page(GFP_HIGHUSER);
  1580. if (!page)
  1581. return -ENOMEM;
  1582. kaddr = kmap_atomic(page);
  1583. memcpy(kaddr, symname, pathlen);
  1584. if (pathlen < PAGE_SIZE)
  1585. memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
  1586. kunmap_atomic(kaddr);
  1587. error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
  1588. if (error != 0) {
  1589. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
  1590. dir->i_sb->s_id, dir->i_ino,
  1591. dentry->d_name.name, symname, error);
  1592. d_drop(dentry);
  1593. __free_page(page);
  1594. return error;
  1595. }
  1596. /*
  1597. * No big deal if we can't add this page to the page cache here.
  1598. * READLINK will get the missing page from the server if needed.
  1599. */
  1600. if (!add_to_page_cache_lru(page, dentry->d_inode->i_mapping, 0,
  1601. GFP_KERNEL)) {
  1602. SetPageUptodate(page);
  1603. unlock_page(page);
  1604. } else
  1605. __free_page(page);
  1606. return 0;
  1607. }
  1608. EXPORT_SYMBOL_GPL(nfs_symlink);
  1609. int
  1610. nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1611. {
  1612. struct inode *inode = old_dentry->d_inode;
  1613. int error;
  1614. dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
  1615. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1616. dentry->d_parent->d_name.name, dentry->d_name.name);
  1617. NFS_PROTO(inode)->return_delegation(inode);
  1618. d_drop(dentry);
  1619. error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
  1620. if (error == 0) {
  1621. ihold(inode);
  1622. d_add(dentry, inode);
  1623. }
  1624. return error;
  1625. }
  1626. EXPORT_SYMBOL_GPL(nfs_link);
  1627. /*
  1628. * RENAME
  1629. * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
  1630. * different file handle for the same inode after a rename (e.g. when
  1631. * moving to a different directory). A fail-safe method to do so would
  1632. * be to look up old_dir/old_name, create a link to new_dir/new_name and
  1633. * rename the old file using the sillyrename stuff. This way, the original
  1634. * file in old_dir will go away when the last process iput()s the inode.
  1635. *
  1636. * FIXED.
  1637. *
  1638. * It actually works quite well. One needs to have the possibility for
  1639. * at least one ".nfs..." file in each directory the file ever gets
  1640. * moved or linked to which happens automagically with the new
  1641. * implementation that only depends on the dcache stuff instead of
  1642. * using the inode layer
  1643. *
  1644. * Unfortunately, things are a little more complicated than indicated
  1645. * above. For a cross-directory move, we want to make sure we can get
  1646. * rid of the old inode after the operation. This means there must be
  1647. * no pending writes (if it's a file), and the use count must be 1.
  1648. * If these conditions are met, we can drop the dentries before doing
  1649. * the rename.
  1650. */
  1651. int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1652. struct inode *new_dir, struct dentry *new_dentry)
  1653. {
  1654. struct inode *old_inode = old_dentry->d_inode;
  1655. struct inode *new_inode = new_dentry->d_inode;
  1656. struct dentry *dentry = NULL, *rehash = NULL;
  1657. int error = -EBUSY;
  1658. dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
  1659. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1660. new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
  1661. d_count(new_dentry));
  1662. /*
  1663. * For non-directories, check whether the target is busy and if so,
  1664. * make a copy of the dentry and then do a silly-rename. If the
  1665. * silly-rename succeeds, the copied dentry is hashed and becomes
  1666. * the new target.
  1667. */
  1668. if (new_inode && !S_ISDIR(new_inode->i_mode)) {
  1669. /*
  1670. * To prevent any new references to the target during the
  1671. * rename, we unhash the dentry in advance.
  1672. */
  1673. if (!d_unhashed(new_dentry)) {
  1674. d_drop(new_dentry);
  1675. rehash = new_dentry;
  1676. }
  1677. if (d_count(new_dentry) > 2) {
  1678. int err;
  1679. /* copy the target dentry's name */
  1680. dentry = d_alloc(new_dentry->d_parent,
  1681. &new_dentry->d_name);
  1682. if (!dentry)
  1683. goto out;
  1684. /* silly-rename the existing target ... */
  1685. err = nfs_sillyrename(new_dir, new_dentry);
  1686. if (err)
  1687. goto out;
  1688. new_dentry = dentry;
  1689. rehash = NULL;
  1690. new_inode = NULL;
  1691. }
  1692. }
  1693. NFS_PROTO(old_inode)->return_delegation(old_inode);
  1694. if (new_inode != NULL)
  1695. NFS_PROTO(new_inode)->return_delegation(new_inode);
  1696. error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
  1697. new_dir, &new_dentry->d_name);
  1698. nfs_mark_for_revalidate(old_inode);
  1699. out:
  1700. if (rehash)
  1701. d_rehash(rehash);
  1702. if (!error) {
  1703. if (new_inode != NULL)
  1704. nfs_drop_nlink(new_inode);
  1705. d_move(old_dentry, new_dentry);
  1706. nfs_set_verifier(new_dentry,
  1707. nfs_save_change_attribute(new_dir));
  1708. } else if (error == -ENOENT)
  1709. nfs_dentry_handle_enoent(old_dentry);
  1710. /* new dentry created? */
  1711. if (dentry)
  1712. dput(dentry);
  1713. return error;
  1714. }
  1715. EXPORT_SYMBOL_GPL(nfs_rename);
  1716. static DEFINE_SPINLOCK(nfs_access_lru_lock);
  1717. static LIST_HEAD(nfs_access_lru_list);
  1718. static atomic_long_t nfs_access_nr_entries;
  1719. static void nfs_access_free_entry(struct nfs_access_entry *entry)
  1720. {
  1721. put_rpccred(entry->cred);
  1722. kfree(entry);
  1723. smp_mb__before_atomic_dec();
  1724. atomic_long_dec(&nfs_access_nr_entries);
  1725. smp_mb__after_atomic_dec();
  1726. }
  1727. static void nfs_access_free_list(struct list_head *head)
  1728. {
  1729. struct nfs_access_entry *cache;
  1730. while (!list_empty(head)) {
  1731. cache = list_entry(head->next, struct nfs_access_entry, lru);
  1732. list_del(&cache->lru);
  1733. nfs_access_free_entry(cache);
  1734. }
  1735. }
  1736. int nfs_access_cache_shrinker(struct shrinker *shrink,
  1737. struct shrink_control *sc)
  1738. {
  1739. LIST_HEAD(head);
  1740. struct nfs_inode *nfsi, *next;
  1741. struct nfs_access_entry *cache;
  1742. int nr_to_scan = sc->nr_to_scan;
  1743. gfp_t gfp_mask = sc->gfp_mask;
  1744. if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
  1745. return (nr_to_scan == 0) ? 0 : -1;
  1746. spin_lock(&nfs_access_lru_lock);
  1747. list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
  1748. struct inode *inode;
  1749. if (nr_to_scan-- == 0)
  1750. break;
  1751. inode = &nfsi->vfs_inode;
  1752. spin_lock(&inode->i_lock);
  1753. if (list_empty(&nfsi->access_cache_entry_lru))
  1754. goto remove_lru_entry;
  1755. cache = list_entry(nfsi->access_cache_entry_lru.next,
  1756. struct nfs_access_entry, lru);
  1757. list_move(&cache->lru, &head);
  1758. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1759. if (!list_empty(&nfsi->access_cache_entry_lru))
  1760. list_move_tail(&nfsi->access_cache_inode_lru,
  1761. &nfs_access_lru_list);
  1762. else {
  1763. remove_lru_entry:
  1764. list_del_init(&nfsi->access_cache_inode_lru);
  1765. smp_mb__before_clear_bit();
  1766. clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
  1767. smp_mb__after_clear_bit();
  1768. }
  1769. spin_unlock(&inode->i_lock);
  1770. }
  1771. spin_unlock(&nfs_access_lru_lock);
  1772. nfs_access_free_list(&head);
  1773. return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
  1774. }
  1775. static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
  1776. {
  1777. struct rb_root *root_node = &nfsi->access_cache;
  1778. struct rb_node *n;
  1779. struct nfs_access_entry *entry;
  1780. /* Unhook entries from the cache */
  1781. while ((n = rb_first(root_node)) != NULL) {
  1782. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1783. rb_erase(n, root_node);
  1784. list_move(&entry->lru, head);
  1785. }
  1786. nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
  1787. }
  1788. void nfs_access_zap_cache(struct inode *inode)
  1789. {
  1790. LIST_HEAD(head);
  1791. if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
  1792. return;
  1793. /* Remove from global LRU init */
  1794. spin_lock(&nfs_access_lru_lock);
  1795. if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  1796. list_del_init(&NFS_I(inode)->access_cache_inode_lru);
  1797. spin_lock(&inode->i_lock);
  1798. __nfs_access_zap_cache(NFS_I(inode), &head);
  1799. spin_unlock(&inode->i_lock);
  1800. spin_unlock(&nfs_access_lru_lock);
  1801. nfs_access_free_list(&head);
  1802. }
  1803. EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
  1804. static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
  1805. {
  1806. struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
  1807. struct nfs_access_entry *entry;
  1808. while (n != NULL) {
  1809. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1810. if (cred < entry->cred)
  1811. n = n->rb_left;
  1812. else if (cred > entry->cred)
  1813. n = n->rb_right;
  1814. else
  1815. return entry;
  1816. }
  1817. return NULL;
  1818. }
  1819. static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
  1820. {
  1821. struct nfs_inode *nfsi = NFS_I(inode);
  1822. struct nfs_access_entry *cache;
  1823. int err = -ENOENT;
  1824. spin_lock(&inode->i_lock);
  1825. if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
  1826. goto out_zap;
  1827. cache = nfs_access_search_rbtree(inode, cred);
  1828. if (cache == NULL)
  1829. goto out;
  1830. if (!nfs_have_delegated_attributes(inode) &&
  1831. !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
  1832. goto out_stale;
  1833. res->jiffies = cache->jiffies;
  1834. res->cred = cache->cred;
  1835. res->mask = cache->mask;
  1836. list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
  1837. err = 0;
  1838. out:
  1839. spin_unlock(&inode->i_lock);
  1840. return err;
  1841. out_stale:
  1842. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1843. list_del(&cache->lru);
  1844. spin_unlock(&inode->i_lock);
  1845. nfs_access_free_entry(cache);
  1846. return -ENOENT;
  1847. out_zap:
  1848. spin_unlock(&inode->i_lock);
  1849. nfs_access_zap_cache(inode);
  1850. return -ENOENT;
  1851. }
  1852. static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
  1853. {
  1854. struct nfs_inode *nfsi = NFS_I(inode);
  1855. struct rb_root *root_node = &nfsi->access_cache;
  1856. struct rb_node **p = &root_node->rb_node;
  1857. struct rb_node *parent = NULL;
  1858. struct nfs_access_entry *entry;
  1859. spin_lock(&inode->i_lock);
  1860. while (*p != NULL) {
  1861. parent = *p;
  1862. entry = rb_entry(parent, struct nfs_access_entry, rb_node);
  1863. if (set->cred < entry->cred)
  1864. p = &parent->rb_left;
  1865. else if (set->cred > entry->cred)
  1866. p = &parent->rb_right;
  1867. else
  1868. goto found;
  1869. }
  1870. rb_link_node(&set->rb_node, parent, p);
  1871. rb_insert_color(&set->rb_node, root_node);
  1872. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1873. spin_unlock(&inode->i_lock);
  1874. return;
  1875. found:
  1876. rb_replace_node(parent, &set->rb_node, root_node);
  1877. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1878. list_del(&entry->lru);
  1879. spin_unlock(&inode->i_lock);
  1880. nfs_access_free_entry(entry);
  1881. }
  1882. void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
  1883. {
  1884. struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
  1885. if (cache == NULL)
  1886. return;
  1887. RB_CLEAR_NODE(&cache->rb_node);
  1888. cache->jiffies = set->jiffies;
  1889. cache->cred = get_rpccred(set->cred);
  1890. cache->mask = set->mask;
  1891. nfs_access_add_rbtree(inode, cache);
  1892. /* Update accounting */
  1893. smp_mb__before_atomic_inc();
  1894. atomic_long_inc(&nfs_access_nr_entries);
  1895. smp_mb__after_atomic_inc();
  1896. /* Add inode to global LRU list */
  1897. if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
  1898. spin_lock(&nfs_access_lru_lock);
  1899. if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  1900. list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
  1901. &nfs_access_lru_list);
  1902. spin_unlock(&nfs_access_lru_lock);
  1903. }
  1904. }
  1905. EXPORT_SYMBOL_GPL(nfs_access_add_cache);
  1906. void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
  1907. {
  1908. entry->mask = 0;
  1909. if (access_result & NFS4_ACCESS_READ)
  1910. entry->mask |= MAY_READ;
  1911. if (access_result &
  1912. (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
  1913. entry->mask |= MAY_WRITE;
  1914. if (access_result & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
  1915. entry->mask |= MAY_EXEC;
  1916. }
  1917. EXPORT_SYMBOL_GPL(nfs_access_set_mask);
  1918. static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
  1919. {
  1920. struct nfs_access_entry cache;
  1921. int status;
  1922. status = nfs_access_get_cached(inode, cred, &cache);
  1923. if (status == 0)
  1924. goto out;
  1925. /* Be clever: ask server to check for all possible rights */
  1926. cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
  1927. cache.cred = cred;
  1928. cache.jiffies = jiffies;
  1929. status = NFS_PROTO(inode)->access(inode, &cache);
  1930. if (status != 0) {
  1931. if (status == -ESTALE) {
  1932. nfs_zap_caches(inode);
  1933. if (!S_ISDIR(inode->i_mode))
  1934. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  1935. }
  1936. return status;
  1937. }
  1938. nfs_access_add_cache(inode, &cache);
  1939. out:
  1940. if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  1941. return 0;
  1942. return -EACCES;
  1943. }
  1944. static int nfs_open_permission_mask(int openflags)
  1945. {
  1946. int mask = 0;
  1947. if (openflags & __FMODE_EXEC) {
  1948. /* ONLY check exec rights */
  1949. mask = MAY_EXEC;
  1950. } else {
  1951. if ((openflags & O_ACCMODE) != O_WRONLY)
  1952. mask |= MAY_READ;
  1953. if ((openflags & O_ACCMODE) != O_RDONLY)
  1954. mask |= MAY_WRITE;
  1955. }
  1956. return mask;
  1957. }
  1958. int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
  1959. {
  1960. return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
  1961. }
  1962. EXPORT_SYMBOL_GPL(nfs_may_open);
  1963. int nfs_permission(struct inode *inode, int mask)
  1964. {
  1965. struct rpc_cred *cred;
  1966. int res = 0;
  1967. if (mask & MAY_NOT_BLOCK)
  1968. return -ECHILD;
  1969. nfs_inc_stats(inode, NFSIOS_VFSACCESS);
  1970. if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  1971. goto out;
  1972. /* Is this sys_access() ? */
  1973. if (mask & (MAY_ACCESS | MAY_CHDIR))
  1974. goto force_lookup;
  1975. switch (inode->i_mode & S_IFMT) {
  1976. case S_IFLNK:
  1977. goto out;
  1978. case S_IFREG:
  1979. break;
  1980. case S_IFDIR:
  1981. /*
  1982. * Optimize away all write operations, since the server
  1983. * will check permissions when we perform the op.
  1984. */
  1985. if ((mask & MAY_WRITE) && !(mask & MAY_READ))
  1986. goto out;
  1987. }
  1988. force_lookup:
  1989. if (!NFS_PROTO(inode)->access)
  1990. goto out_notsup;
  1991. cred = rpc_lookup_cred();
  1992. if (!IS_ERR(cred)) {
  1993. res = nfs_do_access(inode, cred, mask);
  1994. put_rpccred(cred);
  1995. } else
  1996. res = PTR_ERR(cred);
  1997. out:
  1998. if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
  1999. res = -EACCES;
  2000. dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
  2001. inode->i_sb->s_id, inode->i_ino, mask, res);
  2002. return res;
  2003. out_notsup:
  2004. res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  2005. if (res == 0)
  2006. res = generic_permission(inode, mask);
  2007. goto out;
  2008. }
  2009. EXPORT_SYMBOL_GPL(nfs_permission);
  2010. /*
  2011. * Local variables:
  2012. * version-control: t
  2013. * kept-new-versions: 5
  2014. * End:
  2015. */