dir.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096
  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 "nfs4_fs.h"
  36. #include "delegation.h"
  37. #include "iostat.h"
  38. #include "internal.h"
  39. #include "fscache.h"
  40. /* #define NFS_DEBUG_VERBOSE 1 */
  41. static int nfs_opendir(struct inode *, struct file *);
  42. static int nfs_readdir(struct file *, void *, filldir_t);
  43. static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
  44. static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
  45. static int nfs_mkdir(struct inode *, struct dentry *, int);
  46. static int nfs_rmdir(struct inode *, struct dentry *);
  47. static int nfs_unlink(struct inode *, struct dentry *);
  48. static int nfs_symlink(struct inode *, struct dentry *, const char *);
  49. static int nfs_link(struct dentry *, struct inode *, struct dentry *);
  50. static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
  51. static int nfs_rename(struct inode *, struct dentry *,
  52. struct inode *, struct dentry *);
  53. static int nfs_fsync_dir(struct file *, int);
  54. static loff_t nfs_llseek_dir(struct file *, loff_t, int);
  55. const struct file_operations nfs_dir_operations = {
  56. .llseek = nfs_llseek_dir,
  57. .read = generic_read_dir,
  58. .readdir = nfs_readdir,
  59. .open = nfs_opendir,
  60. .release = nfs_release,
  61. .fsync = nfs_fsync_dir,
  62. };
  63. const struct inode_operations nfs_dir_inode_operations = {
  64. .create = nfs_create,
  65. .lookup = nfs_lookup,
  66. .link = nfs_link,
  67. .unlink = nfs_unlink,
  68. .symlink = nfs_symlink,
  69. .mkdir = nfs_mkdir,
  70. .rmdir = nfs_rmdir,
  71. .mknod = nfs_mknod,
  72. .rename = nfs_rename,
  73. .permission = nfs_permission,
  74. .getattr = nfs_getattr,
  75. .setattr = nfs_setattr,
  76. };
  77. #ifdef CONFIG_NFS_V3
  78. const struct inode_operations nfs3_dir_inode_operations = {
  79. .create = nfs_create,
  80. .lookup = nfs_lookup,
  81. .link = nfs_link,
  82. .unlink = nfs_unlink,
  83. .symlink = nfs_symlink,
  84. .mkdir = nfs_mkdir,
  85. .rmdir = nfs_rmdir,
  86. .mknod = nfs_mknod,
  87. .rename = nfs_rename,
  88. .permission = nfs_permission,
  89. .getattr = nfs_getattr,
  90. .setattr = nfs_setattr,
  91. .listxattr = nfs3_listxattr,
  92. .getxattr = nfs3_getxattr,
  93. .setxattr = nfs3_setxattr,
  94. .removexattr = nfs3_removexattr,
  95. };
  96. #endif /* CONFIG_NFS_V3 */
  97. #ifdef CONFIG_NFS_V4
  98. static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
  99. const struct inode_operations nfs4_dir_inode_operations = {
  100. .create = nfs_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. dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
  985. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  986. /* Check that we are indeed trying to open this file */
  987. if (!is_atomic_open(nd))
  988. goto no_open;
  989. if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
  990. res = ERR_PTR(-ENAMETOOLONG);
  991. goto out;
  992. }
  993. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  994. /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
  995. * the dentry. */
  996. if (nd->flags & LOOKUP_EXCL) {
  997. d_instantiate(dentry, NULL);
  998. goto out;
  999. }
  1000. ctx = nameidata_to_nfs_open_context(dentry, nd);
  1001. res = ERR_CAST(ctx);
  1002. if (IS_ERR(ctx))
  1003. goto out;
  1004. open_flags = nd->intent.open.flags;
  1005. if (nd->flags & LOOKUP_CREATE) {
  1006. attr.ia_mode = nd->intent.open.create_mode;
  1007. attr.ia_valid = ATTR_MODE;
  1008. if (!IS_POSIXACL(dir))
  1009. attr.ia_mode &= ~current_umask();
  1010. } else {
  1011. open_flags &= ~O_EXCL;
  1012. attr.ia_valid = 0;
  1013. BUG_ON(open_flags & O_CREAT);
  1014. }
  1015. /* Open the file on the server */
  1016. nfs_block_sillyrename(dentry->d_parent);
  1017. inode = nfs4_atomic_open(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. if (res != NULL) {
  1042. dput(ctx->path.dentry);
  1043. ctx->path.dentry = dget(res);
  1044. dentry = res;
  1045. }
  1046. nfs_intent_set_file(nd, ctx);
  1047. nfs_unblock_sillyrename(dentry->d_parent);
  1048. out:
  1049. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1050. return res;
  1051. no_open:
  1052. return nfs_lookup(dir, dentry, nd);
  1053. }
  1054. static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
  1055. {
  1056. struct dentry *parent = NULL;
  1057. struct inode *inode = dentry->d_inode;
  1058. struct inode *dir;
  1059. struct nfs_open_context *ctx;
  1060. int openflags, ret = 0;
  1061. if (!is_atomic_open(nd) || d_mountpoint(dentry))
  1062. goto no_open;
  1063. parent = dget_parent(dentry);
  1064. dir = parent->d_inode;
  1065. /* We can't create new files in nfs_open_revalidate(), so we
  1066. * optimize away revalidation of negative dentries.
  1067. */
  1068. if (inode == NULL) {
  1069. if (!nfs_neg_need_reval(dir, dentry, nd))
  1070. ret = 1;
  1071. goto out;
  1072. }
  1073. /* NFS only supports OPEN on regular files */
  1074. if (!S_ISREG(inode->i_mode))
  1075. goto no_open_dput;
  1076. openflags = nd->intent.open.flags;
  1077. /* We cannot do exclusive creation on a positive dentry */
  1078. if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  1079. goto no_open_dput;
  1080. /* We can't create new files, or truncate existing ones here */
  1081. openflags &= ~(O_CREAT|O_EXCL|O_TRUNC);
  1082. ctx = nameidata_to_nfs_open_context(dentry, nd);
  1083. ret = PTR_ERR(ctx);
  1084. if (IS_ERR(ctx))
  1085. goto out;
  1086. /*
  1087. * Note: we're not holding inode->i_mutex and so may be racing with
  1088. * operations that change the directory. We therefore save the
  1089. * change attribute *before* we do the RPC call.
  1090. */
  1091. ret = nfs4_open_revalidate(dir, ctx, openflags);
  1092. if (ret == 1)
  1093. nfs_intent_set_file(nd, ctx);
  1094. else
  1095. put_nfs_open_context(ctx);
  1096. out:
  1097. dput(parent);
  1098. if (!ret)
  1099. d_drop(dentry);
  1100. return ret;
  1101. no_open_dput:
  1102. dput(parent);
  1103. no_open:
  1104. return nfs_lookup_revalidate(dentry, nd);
  1105. }
  1106. #endif /* CONFIG_NFSV4 */
  1107. static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc)
  1108. {
  1109. struct dentry *parent = desc->file->f_path.dentry;
  1110. struct inode *dir = parent->d_inode;
  1111. struct nfs_entry *entry = desc->entry;
  1112. struct dentry *dentry, *alias;
  1113. struct qstr name = {
  1114. .name = entry->name,
  1115. .len = entry->len,
  1116. };
  1117. struct inode *inode;
  1118. unsigned long verf = nfs_save_change_attribute(dir);
  1119. switch (name.len) {
  1120. case 2:
  1121. if (name.name[0] == '.' && name.name[1] == '.')
  1122. return dget_parent(parent);
  1123. break;
  1124. case 1:
  1125. if (name.name[0] == '.')
  1126. return dget(parent);
  1127. }
  1128. spin_lock(&dir->i_lock);
  1129. if (NFS_I(dir)->cache_validity & NFS_INO_INVALID_DATA) {
  1130. spin_unlock(&dir->i_lock);
  1131. return NULL;
  1132. }
  1133. spin_unlock(&dir->i_lock);
  1134. name.hash = full_name_hash(name.name, name.len);
  1135. dentry = d_lookup(parent, &name);
  1136. if (dentry != NULL) {
  1137. /* Is this a positive dentry that matches the readdir info? */
  1138. if (dentry->d_inode != NULL &&
  1139. (NFS_FILEID(dentry->d_inode) == entry->ino ||
  1140. d_mountpoint(dentry))) {
  1141. if (!desc->plus || entry->fh->size == 0)
  1142. return dentry;
  1143. if (nfs_compare_fh(NFS_FH(dentry->d_inode),
  1144. entry->fh) == 0)
  1145. goto out_renew;
  1146. }
  1147. /* No, so d_drop to allow one to be created */
  1148. d_drop(dentry);
  1149. dput(dentry);
  1150. }
  1151. if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR))
  1152. return NULL;
  1153. if (name.len > NFS_SERVER(dir)->namelen)
  1154. return NULL;
  1155. /* Note: caller is already holding the dir->i_mutex! */
  1156. dentry = d_alloc(parent, &name);
  1157. if (dentry == NULL)
  1158. return NULL;
  1159. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  1160. inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
  1161. if (IS_ERR(inode)) {
  1162. dput(dentry);
  1163. return NULL;
  1164. }
  1165. alias = d_materialise_unique(dentry, inode);
  1166. if (alias != NULL) {
  1167. dput(dentry);
  1168. if (IS_ERR(alias))
  1169. return NULL;
  1170. dentry = alias;
  1171. }
  1172. out_renew:
  1173. nfs_set_verifier(dentry, verf);
  1174. return dentry;
  1175. }
  1176. /*
  1177. * Code common to create, mkdir, and mknod.
  1178. */
  1179. int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
  1180. struct nfs_fattr *fattr)
  1181. {
  1182. struct dentry *parent = dget_parent(dentry);
  1183. struct inode *dir = parent->d_inode;
  1184. struct inode *inode;
  1185. int error = -EACCES;
  1186. d_drop(dentry);
  1187. /* We may have been initialized further down */
  1188. if (dentry->d_inode)
  1189. goto out;
  1190. if (fhandle->size == 0) {
  1191. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
  1192. if (error)
  1193. goto out_error;
  1194. }
  1195. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1196. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  1197. struct nfs_server *server = NFS_SB(dentry->d_sb);
  1198. error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
  1199. if (error < 0)
  1200. goto out_error;
  1201. }
  1202. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  1203. error = PTR_ERR(inode);
  1204. if (IS_ERR(inode))
  1205. goto out_error;
  1206. d_add(dentry, inode);
  1207. out:
  1208. dput(parent);
  1209. return 0;
  1210. out_error:
  1211. nfs_mark_for_revalidate(dir);
  1212. dput(parent);
  1213. return error;
  1214. }
  1215. /*
  1216. * Following a failed create operation, we drop the dentry rather
  1217. * than retain a negative dentry. This avoids a problem in the event
  1218. * that the operation succeeded on the server, but an error in the
  1219. * reply path made it appear to have failed.
  1220. */
  1221. static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1222. struct nameidata *nd)
  1223. {
  1224. struct iattr attr;
  1225. int error;
  1226. int open_flags = 0;
  1227. dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
  1228. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1229. attr.ia_mode = mode;
  1230. attr.ia_valid = ATTR_MODE;
  1231. if ((nd->flags & LOOKUP_CREATE) != 0)
  1232. open_flags = nd->intent.open.flags;
  1233. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd);
  1234. if (error != 0)
  1235. goto out_err;
  1236. return 0;
  1237. out_err:
  1238. d_drop(dentry);
  1239. return error;
  1240. }
  1241. /*
  1242. * See comments for nfs_proc_create regarding failed operations.
  1243. */
  1244. static int
  1245. nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
  1246. {
  1247. struct iattr attr;
  1248. int status;
  1249. dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
  1250. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1251. if (!new_valid_dev(rdev))
  1252. return -EINVAL;
  1253. attr.ia_mode = mode;
  1254. attr.ia_valid = ATTR_MODE;
  1255. status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
  1256. if (status != 0)
  1257. goto out_err;
  1258. return 0;
  1259. out_err:
  1260. d_drop(dentry);
  1261. return status;
  1262. }
  1263. /*
  1264. * See comments for nfs_proc_create regarding failed operations.
  1265. */
  1266. static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1267. {
  1268. struct iattr attr;
  1269. int error;
  1270. dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
  1271. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1272. attr.ia_valid = ATTR_MODE;
  1273. attr.ia_mode = mode | S_IFDIR;
  1274. error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
  1275. if (error != 0)
  1276. goto out_err;
  1277. return 0;
  1278. out_err:
  1279. d_drop(dentry);
  1280. return error;
  1281. }
  1282. static void nfs_dentry_handle_enoent(struct dentry *dentry)
  1283. {
  1284. if (dentry->d_inode != NULL && !d_unhashed(dentry))
  1285. d_delete(dentry);
  1286. }
  1287. static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
  1288. {
  1289. int error;
  1290. dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
  1291. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1292. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  1293. /* Ensure the VFS deletes this inode */
  1294. if (error == 0 && dentry->d_inode != NULL)
  1295. clear_nlink(dentry->d_inode);
  1296. else if (error == -ENOENT)
  1297. nfs_dentry_handle_enoent(dentry);
  1298. return error;
  1299. }
  1300. static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
  1301. {
  1302. static unsigned int sillycounter;
  1303. const int fileidsize = sizeof(NFS_FILEID(dentry->d_inode))*2;
  1304. const int countersize = sizeof(sillycounter)*2;
  1305. const int slen = sizeof(".nfs")+fileidsize+countersize-1;
  1306. char silly[slen+1];
  1307. struct qstr qsilly;
  1308. struct dentry *sdentry;
  1309. int error = -EIO;
  1310. dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
  1311. dentry->d_parent->d_name.name, dentry->d_name.name,
  1312. atomic_read(&dentry->d_count));
  1313. nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
  1314. /*
  1315. * We don't allow a dentry to be silly-renamed twice.
  1316. */
  1317. error = -EBUSY;
  1318. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1319. goto out;
  1320. sprintf(silly, ".nfs%*.*Lx",
  1321. fileidsize, fileidsize,
  1322. (unsigned long long)NFS_FILEID(dentry->d_inode));
  1323. /* Return delegation in anticipation of the rename */
  1324. nfs_inode_return_delegation(dentry->d_inode);
  1325. sdentry = NULL;
  1326. do {
  1327. char *suffix = silly + slen - countersize;
  1328. dput(sdentry);
  1329. sillycounter++;
  1330. sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
  1331. dfprintk(VFS, "NFS: trying to rename %s to %s\n",
  1332. dentry->d_name.name, silly);
  1333. sdentry = lookup_one_len(silly, dentry->d_parent, slen);
  1334. /*
  1335. * N.B. Better to return EBUSY here ... it could be
  1336. * dangerous to delete the file while it's in use.
  1337. */
  1338. if (IS_ERR(sdentry))
  1339. goto out;
  1340. } while(sdentry->d_inode != NULL); /* need negative lookup */
  1341. qsilly.name = silly;
  1342. qsilly.len = strlen(silly);
  1343. if (dentry->d_inode) {
  1344. error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
  1345. dir, &qsilly);
  1346. nfs_mark_for_revalidate(dentry->d_inode);
  1347. } else
  1348. error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
  1349. dir, &qsilly);
  1350. if (!error) {
  1351. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1352. d_move(dentry, sdentry);
  1353. error = nfs_async_unlink(dir, dentry);
  1354. /* If we return 0 we don't unlink */
  1355. }
  1356. dput(sdentry);
  1357. out:
  1358. return error;
  1359. }
  1360. /*
  1361. * Remove a file after making sure there are no pending writes,
  1362. * and after checking that the file has only one user.
  1363. *
  1364. * We invalidate the attribute cache and free the inode prior to the operation
  1365. * to avoid possible races if the server reuses the inode.
  1366. */
  1367. static int nfs_safe_remove(struct dentry *dentry)
  1368. {
  1369. struct inode *dir = dentry->d_parent->d_inode;
  1370. struct inode *inode = dentry->d_inode;
  1371. int error = -EBUSY;
  1372. dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
  1373. dentry->d_parent->d_name.name, dentry->d_name.name);
  1374. /* If the dentry was sillyrenamed, we simply call d_delete() */
  1375. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1376. error = 0;
  1377. goto out;
  1378. }
  1379. if (inode != NULL) {
  1380. nfs_inode_return_delegation(inode);
  1381. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1382. /* The VFS may want to delete this inode */
  1383. if (error == 0)
  1384. nfs_drop_nlink(inode);
  1385. nfs_mark_for_revalidate(inode);
  1386. } else
  1387. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1388. if (error == -ENOENT)
  1389. nfs_dentry_handle_enoent(dentry);
  1390. out:
  1391. return error;
  1392. }
  1393. /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
  1394. * belongs to an active ".nfs..." file and we return -EBUSY.
  1395. *
  1396. * If sillyrename() returns 0, we do nothing, otherwise we unlink.
  1397. */
  1398. static int nfs_unlink(struct inode *dir, struct dentry *dentry)
  1399. {
  1400. int error;
  1401. int need_rehash = 0;
  1402. dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
  1403. dir->i_ino, dentry->d_name.name);
  1404. spin_lock(&dcache_lock);
  1405. spin_lock(&dentry->d_lock);
  1406. if (atomic_read(&dentry->d_count) > 1) {
  1407. spin_unlock(&dentry->d_lock);
  1408. spin_unlock(&dcache_lock);
  1409. /* Start asynchronous writeout of the inode */
  1410. write_inode_now(dentry->d_inode, 0);
  1411. error = nfs_sillyrename(dir, dentry);
  1412. return error;
  1413. }
  1414. if (!d_unhashed(dentry)) {
  1415. __d_drop(dentry);
  1416. need_rehash = 1;
  1417. }
  1418. spin_unlock(&dentry->d_lock);
  1419. spin_unlock(&dcache_lock);
  1420. error = nfs_safe_remove(dentry);
  1421. if (!error || error == -ENOENT) {
  1422. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1423. } else if (need_rehash)
  1424. d_rehash(dentry);
  1425. return error;
  1426. }
  1427. /*
  1428. * To create a symbolic link, most file systems instantiate a new inode,
  1429. * add a page to it containing the path, then write it out to the disk
  1430. * using prepare_write/commit_write.
  1431. *
  1432. * Unfortunately the NFS client can't create the in-core inode first
  1433. * because it needs a file handle to create an in-core inode (see
  1434. * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
  1435. * symlink request has completed on the server.
  1436. *
  1437. * So instead we allocate a raw page, copy the symname into it, then do
  1438. * the SYMLINK request with the page as the buffer. If it succeeds, we
  1439. * now have a new file handle and can instantiate an in-core NFS inode
  1440. * and move the raw page into its mapping.
  1441. */
  1442. static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1443. {
  1444. struct pagevec lru_pvec;
  1445. struct page *page;
  1446. char *kaddr;
  1447. struct iattr attr;
  1448. unsigned int pathlen = strlen(symname);
  1449. int error;
  1450. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
  1451. dir->i_ino, dentry->d_name.name, symname);
  1452. if (pathlen > PAGE_SIZE)
  1453. return -ENAMETOOLONG;
  1454. attr.ia_mode = S_IFLNK | S_IRWXUGO;
  1455. attr.ia_valid = ATTR_MODE;
  1456. page = alloc_page(GFP_HIGHUSER);
  1457. if (!page)
  1458. return -ENOMEM;
  1459. kaddr = kmap_atomic(page, KM_USER0);
  1460. memcpy(kaddr, symname, pathlen);
  1461. if (pathlen < PAGE_SIZE)
  1462. memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
  1463. kunmap_atomic(kaddr, KM_USER0);
  1464. error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
  1465. if (error != 0) {
  1466. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
  1467. dir->i_sb->s_id, dir->i_ino,
  1468. dentry->d_name.name, symname, error);
  1469. d_drop(dentry);
  1470. __free_page(page);
  1471. return error;
  1472. }
  1473. /*
  1474. * No big deal if we can't add this page to the page cache here.
  1475. * READLINK will get the missing page from the server if needed.
  1476. */
  1477. pagevec_init(&lru_pvec, 0);
  1478. if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
  1479. GFP_KERNEL)) {
  1480. pagevec_add(&lru_pvec, page);
  1481. pagevec_lru_add_file(&lru_pvec);
  1482. SetPageUptodate(page);
  1483. unlock_page(page);
  1484. } else
  1485. __free_page(page);
  1486. return 0;
  1487. }
  1488. static int
  1489. nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1490. {
  1491. struct inode *inode = old_dentry->d_inode;
  1492. int error;
  1493. dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
  1494. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1495. dentry->d_parent->d_name.name, dentry->d_name.name);
  1496. nfs_inode_return_delegation(inode);
  1497. d_drop(dentry);
  1498. error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
  1499. if (error == 0) {
  1500. atomic_inc(&inode->i_count);
  1501. d_add(dentry, inode);
  1502. }
  1503. return error;
  1504. }
  1505. /*
  1506. * RENAME
  1507. * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
  1508. * different file handle for the same inode after a rename (e.g. when
  1509. * moving to a different directory). A fail-safe method to do so would
  1510. * be to look up old_dir/old_name, create a link to new_dir/new_name and
  1511. * rename the old file using the sillyrename stuff. This way, the original
  1512. * file in old_dir will go away when the last process iput()s the inode.
  1513. *
  1514. * FIXED.
  1515. *
  1516. * It actually works quite well. One needs to have the possibility for
  1517. * at least one ".nfs..." file in each directory the file ever gets
  1518. * moved or linked to which happens automagically with the new
  1519. * implementation that only depends on the dcache stuff instead of
  1520. * using the inode layer
  1521. *
  1522. * Unfortunately, things are a little more complicated than indicated
  1523. * above. For a cross-directory move, we want to make sure we can get
  1524. * rid of the old inode after the operation. This means there must be
  1525. * no pending writes (if it's a file), and the use count must be 1.
  1526. * If these conditions are met, we can drop the dentries before doing
  1527. * the rename.
  1528. */
  1529. static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1530. struct inode *new_dir, struct dentry *new_dentry)
  1531. {
  1532. struct inode *old_inode = old_dentry->d_inode;
  1533. struct inode *new_inode = new_dentry->d_inode;
  1534. struct dentry *dentry = NULL, *rehash = NULL;
  1535. int error = -EBUSY;
  1536. dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
  1537. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1538. new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
  1539. atomic_read(&new_dentry->d_count));
  1540. /*
  1541. * For non-directories, check whether the target is busy and if so,
  1542. * make a copy of the dentry and then do a silly-rename. If the
  1543. * silly-rename succeeds, the copied dentry is hashed and becomes
  1544. * the new target.
  1545. */
  1546. if (new_inode && !S_ISDIR(new_inode->i_mode)) {
  1547. /*
  1548. * To prevent any new references to the target during the
  1549. * rename, we unhash the dentry in advance.
  1550. */
  1551. if (!d_unhashed(new_dentry)) {
  1552. d_drop(new_dentry);
  1553. rehash = new_dentry;
  1554. }
  1555. if (atomic_read(&new_dentry->d_count) > 2) {
  1556. int err;
  1557. /* copy the target dentry's name */
  1558. dentry = d_alloc(new_dentry->d_parent,
  1559. &new_dentry->d_name);
  1560. if (!dentry)
  1561. goto out;
  1562. /* silly-rename the existing target ... */
  1563. err = nfs_sillyrename(new_dir, new_dentry);
  1564. if (err)
  1565. goto out;
  1566. new_dentry = dentry;
  1567. rehash = NULL;
  1568. new_inode = NULL;
  1569. }
  1570. }
  1571. nfs_inode_return_delegation(old_inode);
  1572. if (new_inode != NULL)
  1573. nfs_inode_return_delegation(new_inode);
  1574. error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
  1575. new_dir, &new_dentry->d_name);
  1576. nfs_mark_for_revalidate(old_inode);
  1577. out:
  1578. if (rehash)
  1579. d_rehash(rehash);
  1580. if (!error) {
  1581. if (new_inode != NULL)
  1582. nfs_drop_nlink(new_inode);
  1583. d_move(old_dentry, new_dentry);
  1584. nfs_set_verifier(new_dentry,
  1585. nfs_save_change_attribute(new_dir));
  1586. } else if (error == -ENOENT)
  1587. nfs_dentry_handle_enoent(old_dentry);
  1588. /* new dentry created? */
  1589. if (dentry)
  1590. dput(dentry);
  1591. return error;
  1592. }
  1593. static DEFINE_SPINLOCK(nfs_access_lru_lock);
  1594. static LIST_HEAD(nfs_access_lru_list);
  1595. static atomic_long_t nfs_access_nr_entries;
  1596. static void nfs_access_free_entry(struct nfs_access_entry *entry)
  1597. {
  1598. put_rpccred(entry->cred);
  1599. kfree(entry);
  1600. smp_mb__before_atomic_dec();
  1601. atomic_long_dec(&nfs_access_nr_entries);
  1602. smp_mb__after_atomic_dec();
  1603. }
  1604. static void nfs_access_free_list(struct list_head *head)
  1605. {
  1606. struct nfs_access_entry *cache;
  1607. while (!list_empty(head)) {
  1608. cache = list_entry(head->next, struct nfs_access_entry, lru);
  1609. list_del(&cache->lru);
  1610. nfs_access_free_entry(cache);
  1611. }
  1612. }
  1613. int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
  1614. {
  1615. LIST_HEAD(head);
  1616. struct nfs_inode *nfsi;
  1617. struct nfs_access_entry *cache;
  1618. if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
  1619. return (nr_to_scan == 0) ? 0 : -1;
  1620. spin_lock(&nfs_access_lru_lock);
  1621. list_for_each_entry(nfsi, &nfs_access_lru_list, access_cache_inode_lru) {
  1622. struct inode *inode;
  1623. if (nr_to_scan-- == 0)
  1624. break;
  1625. inode = &nfsi->vfs_inode;
  1626. spin_lock(&inode->i_lock);
  1627. if (list_empty(&nfsi->access_cache_entry_lru))
  1628. goto remove_lru_entry;
  1629. cache = list_entry(nfsi->access_cache_entry_lru.next,
  1630. struct nfs_access_entry, lru);
  1631. list_move(&cache->lru, &head);
  1632. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1633. if (!list_empty(&nfsi->access_cache_entry_lru))
  1634. list_move_tail(&nfsi->access_cache_inode_lru,
  1635. &nfs_access_lru_list);
  1636. else {
  1637. remove_lru_entry:
  1638. list_del_init(&nfsi->access_cache_inode_lru);
  1639. smp_mb__before_clear_bit();
  1640. clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
  1641. smp_mb__after_clear_bit();
  1642. }
  1643. spin_unlock(&inode->i_lock);
  1644. }
  1645. spin_unlock(&nfs_access_lru_lock);
  1646. nfs_access_free_list(&head);
  1647. return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
  1648. }
  1649. static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
  1650. {
  1651. struct rb_root *root_node = &nfsi->access_cache;
  1652. struct rb_node *n;
  1653. struct nfs_access_entry *entry;
  1654. /* Unhook entries from the cache */
  1655. while ((n = rb_first(root_node)) != NULL) {
  1656. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1657. rb_erase(n, root_node);
  1658. list_move(&entry->lru, head);
  1659. }
  1660. nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
  1661. }
  1662. void nfs_access_zap_cache(struct inode *inode)
  1663. {
  1664. LIST_HEAD(head);
  1665. if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
  1666. return;
  1667. /* Remove from global LRU init */
  1668. spin_lock(&nfs_access_lru_lock);
  1669. if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  1670. list_del_init(&NFS_I(inode)->access_cache_inode_lru);
  1671. spin_lock(&inode->i_lock);
  1672. __nfs_access_zap_cache(NFS_I(inode), &head);
  1673. spin_unlock(&inode->i_lock);
  1674. spin_unlock(&nfs_access_lru_lock);
  1675. nfs_access_free_list(&head);
  1676. }
  1677. static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
  1678. {
  1679. struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
  1680. struct nfs_access_entry *entry;
  1681. while (n != NULL) {
  1682. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1683. if (cred < entry->cred)
  1684. n = n->rb_left;
  1685. else if (cred > entry->cred)
  1686. n = n->rb_right;
  1687. else
  1688. return entry;
  1689. }
  1690. return NULL;
  1691. }
  1692. static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
  1693. {
  1694. struct nfs_inode *nfsi = NFS_I(inode);
  1695. struct nfs_access_entry *cache;
  1696. int err = -ENOENT;
  1697. spin_lock(&inode->i_lock);
  1698. if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
  1699. goto out_zap;
  1700. cache = nfs_access_search_rbtree(inode, cred);
  1701. if (cache == NULL)
  1702. goto out;
  1703. if (!nfs_have_delegated_attributes(inode) &&
  1704. !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
  1705. goto out_stale;
  1706. res->jiffies = cache->jiffies;
  1707. res->cred = cache->cred;
  1708. res->mask = cache->mask;
  1709. list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
  1710. err = 0;
  1711. out:
  1712. spin_unlock(&inode->i_lock);
  1713. return err;
  1714. out_stale:
  1715. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1716. list_del(&cache->lru);
  1717. spin_unlock(&inode->i_lock);
  1718. nfs_access_free_entry(cache);
  1719. return -ENOENT;
  1720. out_zap:
  1721. spin_unlock(&inode->i_lock);
  1722. nfs_access_zap_cache(inode);
  1723. return -ENOENT;
  1724. }
  1725. static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
  1726. {
  1727. struct nfs_inode *nfsi = NFS_I(inode);
  1728. struct rb_root *root_node = &nfsi->access_cache;
  1729. struct rb_node **p = &root_node->rb_node;
  1730. struct rb_node *parent = NULL;
  1731. struct nfs_access_entry *entry;
  1732. spin_lock(&inode->i_lock);
  1733. while (*p != NULL) {
  1734. parent = *p;
  1735. entry = rb_entry(parent, struct nfs_access_entry, rb_node);
  1736. if (set->cred < entry->cred)
  1737. p = &parent->rb_left;
  1738. else if (set->cred > entry->cred)
  1739. p = &parent->rb_right;
  1740. else
  1741. goto found;
  1742. }
  1743. rb_link_node(&set->rb_node, parent, p);
  1744. rb_insert_color(&set->rb_node, root_node);
  1745. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1746. spin_unlock(&inode->i_lock);
  1747. return;
  1748. found:
  1749. rb_replace_node(parent, &set->rb_node, root_node);
  1750. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1751. list_del(&entry->lru);
  1752. spin_unlock(&inode->i_lock);
  1753. nfs_access_free_entry(entry);
  1754. }
  1755. static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
  1756. {
  1757. struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
  1758. if (cache == NULL)
  1759. return;
  1760. RB_CLEAR_NODE(&cache->rb_node);
  1761. cache->jiffies = set->jiffies;
  1762. cache->cred = get_rpccred(set->cred);
  1763. cache->mask = set->mask;
  1764. nfs_access_add_rbtree(inode, cache);
  1765. /* Update accounting */
  1766. smp_mb__before_atomic_inc();
  1767. atomic_long_inc(&nfs_access_nr_entries);
  1768. smp_mb__after_atomic_inc();
  1769. /* Add inode to global LRU list */
  1770. if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
  1771. spin_lock(&nfs_access_lru_lock);
  1772. if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  1773. list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
  1774. &nfs_access_lru_list);
  1775. spin_unlock(&nfs_access_lru_lock);
  1776. }
  1777. }
  1778. static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
  1779. {
  1780. struct nfs_access_entry cache;
  1781. int status;
  1782. status = nfs_access_get_cached(inode, cred, &cache);
  1783. if (status == 0)
  1784. goto out;
  1785. /* Be clever: ask server to check for all possible rights */
  1786. cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
  1787. cache.cred = cred;
  1788. cache.jiffies = jiffies;
  1789. status = NFS_PROTO(inode)->access(inode, &cache);
  1790. if (status != 0) {
  1791. if (status == -ESTALE) {
  1792. nfs_zap_caches(inode);
  1793. if (!S_ISDIR(inode->i_mode))
  1794. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  1795. }
  1796. return status;
  1797. }
  1798. nfs_access_add_cache(inode, &cache);
  1799. out:
  1800. if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  1801. return 0;
  1802. return -EACCES;
  1803. }
  1804. static int nfs_open_permission_mask(int openflags)
  1805. {
  1806. int mask = 0;
  1807. if (openflags & FMODE_READ)
  1808. mask |= MAY_READ;
  1809. if (openflags & FMODE_WRITE)
  1810. mask |= MAY_WRITE;
  1811. if (openflags & FMODE_EXEC)
  1812. mask |= MAY_EXEC;
  1813. return mask;
  1814. }
  1815. int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
  1816. {
  1817. return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
  1818. }
  1819. int nfs_permission(struct inode *inode, int mask)
  1820. {
  1821. struct rpc_cred *cred;
  1822. int res = 0;
  1823. nfs_inc_stats(inode, NFSIOS_VFSACCESS);
  1824. if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  1825. goto out;
  1826. /* Is this sys_access() ? */
  1827. if (mask & (MAY_ACCESS | MAY_CHDIR))
  1828. goto force_lookup;
  1829. switch (inode->i_mode & S_IFMT) {
  1830. case S_IFLNK:
  1831. goto out;
  1832. case S_IFREG:
  1833. /* NFSv4 has atomic_open... */
  1834. if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
  1835. && (mask & MAY_OPEN)
  1836. && !(mask & MAY_EXEC))
  1837. goto out;
  1838. break;
  1839. case S_IFDIR:
  1840. /*
  1841. * Optimize away all write operations, since the server
  1842. * will check permissions when we perform the op.
  1843. */
  1844. if ((mask & MAY_WRITE) && !(mask & MAY_READ))
  1845. goto out;
  1846. }
  1847. force_lookup:
  1848. if (!NFS_PROTO(inode)->access)
  1849. goto out_notsup;
  1850. cred = rpc_lookup_cred();
  1851. if (!IS_ERR(cred)) {
  1852. res = nfs_do_access(inode, cred, mask);
  1853. put_rpccred(cred);
  1854. } else
  1855. res = PTR_ERR(cred);
  1856. out:
  1857. if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
  1858. res = -EACCES;
  1859. dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
  1860. inode->i_sb->s_id, inode->i_ino, mask, res);
  1861. return res;
  1862. out_notsup:
  1863. res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  1864. if (res == 0)
  1865. res = generic_permission(inode, mask, NULL);
  1866. goto out;
  1867. }
  1868. /*
  1869. * Local variables:
  1870. * version-control: t
  1871. * kept-new-versions: 5
  1872. * End:
  1873. */