dir.c 64 KB

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