dir.c 53 KB

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