dir.c 55 KB

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