dir.c 53 KB

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