dir.c 52 KB

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