dir.c 46 KB

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