dir.c 63 KB

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