dir.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977
  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. if (!S_ISDIR(old_inode->i_mode))
  1507. d_move(old_dentry, new_dentry);
  1508. nfs_renew_times(new_dentry);
  1509. nfs_set_verifier(new_dentry, nfs_save_change_attribute(new_dir));
  1510. }
  1511. /* new dentry created? */
  1512. if (dentry)
  1513. dput(dentry);
  1514. unlock_kernel();
  1515. return error;
  1516. }
  1517. static DEFINE_SPINLOCK(nfs_access_lru_lock);
  1518. static LIST_HEAD(nfs_access_lru_list);
  1519. static atomic_long_t nfs_access_nr_entries;
  1520. static void nfs_access_free_entry(struct nfs_access_entry *entry)
  1521. {
  1522. put_rpccred(entry->cred);
  1523. kfree(entry);
  1524. smp_mb__before_atomic_dec();
  1525. atomic_long_dec(&nfs_access_nr_entries);
  1526. smp_mb__after_atomic_dec();
  1527. }
  1528. int nfs_access_cache_shrinker(int nr_to_scan, gfp_t gfp_mask)
  1529. {
  1530. LIST_HEAD(head);
  1531. struct nfs_inode *nfsi;
  1532. struct nfs_access_entry *cache;
  1533. spin_lock(&nfs_access_lru_lock);
  1534. restart:
  1535. list_for_each_entry(nfsi, &nfs_access_lru_list, access_cache_inode_lru) {
  1536. struct inode *inode;
  1537. if (nr_to_scan-- == 0)
  1538. break;
  1539. inode = igrab(&nfsi->vfs_inode);
  1540. if (inode == NULL)
  1541. continue;
  1542. spin_lock(&inode->i_lock);
  1543. if (list_empty(&nfsi->access_cache_entry_lru))
  1544. goto remove_lru_entry;
  1545. cache = list_entry(nfsi->access_cache_entry_lru.next,
  1546. struct nfs_access_entry, lru);
  1547. list_move(&cache->lru, &head);
  1548. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1549. if (!list_empty(&nfsi->access_cache_entry_lru))
  1550. list_move_tail(&nfsi->access_cache_inode_lru,
  1551. &nfs_access_lru_list);
  1552. else {
  1553. remove_lru_entry:
  1554. list_del_init(&nfsi->access_cache_inode_lru);
  1555. clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
  1556. }
  1557. spin_unlock(&inode->i_lock);
  1558. iput(inode);
  1559. goto restart;
  1560. }
  1561. spin_unlock(&nfs_access_lru_lock);
  1562. while (!list_empty(&head)) {
  1563. cache = list_entry(head.next, struct nfs_access_entry, lru);
  1564. list_del(&cache->lru);
  1565. nfs_access_free_entry(cache);
  1566. }
  1567. return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
  1568. }
  1569. static void __nfs_access_zap_cache(struct inode *inode)
  1570. {
  1571. struct nfs_inode *nfsi = NFS_I(inode);
  1572. struct rb_root *root_node = &nfsi->access_cache;
  1573. struct rb_node *n, *dispose = NULL;
  1574. struct nfs_access_entry *entry;
  1575. /* Unhook entries from the cache */
  1576. while ((n = rb_first(root_node)) != NULL) {
  1577. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1578. rb_erase(n, root_node);
  1579. list_del(&entry->lru);
  1580. n->rb_left = dispose;
  1581. dispose = n;
  1582. }
  1583. nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
  1584. spin_unlock(&inode->i_lock);
  1585. /* Now kill them all! */
  1586. while (dispose != NULL) {
  1587. n = dispose;
  1588. dispose = n->rb_left;
  1589. nfs_access_free_entry(rb_entry(n, struct nfs_access_entry, rb_node));
  1590. }
  1591. }
  1592. void nfs_access_zap_cache(struct inode *inode)
  1593. {
  1594. /* Remove from global LRU init */
  1595. if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_FLAGS(inode))) {
  1596. spin_lock(&nfs_access_lru_lock);
  1597. list_del_init(&NFS_I(inode)->access_cache_inode_lru);
  1598. spin_unlock(&nfs_access_lru_lock);
  1599. }
  1600. spin_lock(&inode->i_lock);
  1601. /* This will release the spinlock */
  1602. __nfs_access_zap_cache(inode);
  1603. }
  1604. static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
  1605. {
  1606. struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
  1607. struct nfs_access_entry *entry;
  1608. while (n != NULL) {
  1609. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1610. if (cred < entry->cred)
  1611. n = n->rb_left;
  1612. else if (cred > entry->cred)
  1613. n = n->rb_right;
  1614. else
  1615. return entry;
  1616. }
  1617. return NULL;
  1618. }
  1619. int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
  1620. {
  1621. struct nfs_inode *nfsi = NFS_I(inode);
  1622. struct nfs_access_entry *cache;
  1623. int err = -ENOENT;
  1624. spin_lock(&inode->i_lock);
  1625. if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
  1626. goto out_zap;
  1627. cache = nfs_access_search_rbtree(inode, cred);
  1628. if (cache == NULL)
  1629. goto out;
  1630. if (time_after(jiffies, cache->jiffies + NFS_ATTRTIMEO(inode)))
  1631. goto out_stale;
  1632. res->jiffies = cache->jiffies;
  1633. res->cred = cache->cred;
  1634. res->mask = cache->mask;
  1635. list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
  1636. err = 0;
  1637. out:
  1638. spin_unlock(&inode->i_lock);
  1639. return err;
  1640. out_stale:
  1641. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1642. list_del(&cache->lru);
  1643. spin_unlock(&inode->i_lock);
  1644. nfs_access_free_entry(cache);
  1645. return -ENOENT;
  1646. out_zap:
  1647. /* This will release the spinlock */
  1648. __nfs_access_zap_cache(inode);
  1649. return -ENOENT;
  1650. }
  1651. static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
  1652. {
  1653. struct nfs_inode *nfsi = NFS_I(inode);
  1654. struct rb_root *root_node = &nfsi->access_cache;
  1655. struct rb_node **p = &root_node->rb_node;
  1656. struct rb_node *parent = NULL;
  1657. struct nfs_access_entry *entry;
  1658. spin_lock(&inode->i_lock);
  1659. while (*p != NULL) {
  1660. parent = *p;
  1661. entry = rb_entry(parent, struct nfs_access_entry, rb_node);
  1662. if (set->cred < entry->cred)
  1663. p = &parent->rb_left;
  1664. else if (set->cred > entry->cred)
  1665. p = &parent->rb_right;
  1666. else
  1667. goto found;
  1668. }
  1669. rb_link_node(&set->rb_node, parent, p);
  1670. rb_insert_color(&set->rb_node, root_node);
  1671. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1672. spin_unlock(&inode->i_lock);
  1673. return;
  1674. found:
  1675. rb_replace_node(parent, &set->rb_node, root_node);
  1676. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1677. list_del(&entry->lru);
  1678. spin_unlock(&inode->i_lock);
  1679. nfs_access_free_entry(entry);
  1680. }
  1681. void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
  1682. {
  1683. struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
  1684. if (cache == NULL)
  1685. return;
  1686. RB_CLEAR_NODE(&cache->rb_node);
  1687. cache->jiffies = set->jiffies;
  1688. cache->cred = get_rpccred(set->cred);
  1689. cache->mask = set->mask;
  1690. nfs_access_add_rbtree(inode, cache);
  1691. /* Update accounting */
  1692. smp_mb__before_atomic_inc();
  1693. atomic_long_inc(&nfs_access_nr_entries);
  1694. smp_mb__after_atomic_inc();
  1695. /* Add inode to global LRU list */
  1696. if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_FLAGS(inode))) {
  1697. spin_lock(&nfs_access_lru_lock);
  1698. list_add_tail(&NFS_I(inode)->access_cache_inode_lru, &nfs_access_lru_list);
  1699. spin_unlock(&nfs_access_lru_lock);
  1700. }
  1701. }
  1702. static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
  1703. {
  1704. struct nfs_access_entry cache;
  1705. int status;
  1706. status = nfs_access_get_cached(inode, cred, &cache);
  1707. if (status == 0)
  1708. goto out;
  1709. /* Be clever: ask server to check for all possible rights */
  1710. cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
  1711. cache.cred = cred;
  1712. cache.jiffies = jiffies;
  1713. status = NFS_PROTO(inode)->access(inode, &cache);
  1714. if (status != 0)
  1715. return status;
  1716. nfs_access_add_cache(inode, &cache);
  1717. out:
  1718. if ((cache.mask & mask) == mask)
  1719. return 0;
  1720. return -EACCES;
  1721. }
  1722. int nfs_permission(struct inode *inode, int mask, struct nameidata *nd)
  1723. {
  1724. struct rpc_cred *cred;
  1725. int res = 0;
  1726. nfs_inc_stats(inode, NFSIOS_VFSACCESS);
  1727. if (mask == 0)
  1728. goto out;
  1729. /* Is this sys_access() ? */
  1730. if (nd != NULL && (nd->flags & LOOKUP_ACCESS))
  1731. goto force_lookup;
  1732. switch (inode->i_mode & S_IFMT) {
  1733. case S_IFLNK:
  1734. goto out;
  1735. case S_IFREG:
  1736. /* NFSv4 has atomic_open... */
  1737. if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
  1738. && nd != NULL
  1739. && (nd->flags & LOOKUP_OPEN))
  1740. goto out;
  1741. break;
  1742. case S_IFDIR:
  1743. /*
  1744. * Optimize away all write operations, since the server
  1745. * will check permissions when we perform the op.
  1746. */
  1747. if ((mask & MAY_WRITE) && !(mask & MAY_READ))
  1748. goto out;
  1749. }
  1750. force_lookup:
  1751. lock_kernel();
  1752. if (!NFS_PROTO(inode)->access)
  1753. goto out_notsup;
  1754. cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
  1755. if (!IS_ERR(cred)) {
  1756. res = nfs_do_access(inode, cred, mask);
  1757. put_rpccred(cred);
  1758. } else
  1759. res = PTR_ERR(cred);
  1760. unlock_kernel();
  1761. out:
  1762. dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
  1763. inode->i_sb->s_id, inode->i_ino, mask, res);
  1764. return res;
  1765. out_notsup:
  1766. res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  1767. if (res == 0)
  1768. res = generic_permission(inode, mask, NULL);
  1769. unlock_kernel();
  1770. goto out;
  1771. }
  1772. /*
  1773. * Local variables:
  1774. * version-control: t
  1775. * kept-new-versions: 5
  1776. * End:
  1777. */