dir.c 64 KB

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