dir.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347
  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/pagevec.h>
  32. #include <linux/namei.h>
  33. #include <linux/mount.h>
  34. #include <linux/sched.h>
  35. #include <linux/kmemleak.h>
  36. #include <linux/xattr.h>
  37. #include "delegation.h"
  38. #include "iostat.h"
  39. #include "internal.h"
  40. #include "fscache.h"
  41. /* #define NFS_DEBUG_VERBOSE 1 */
  42. static int nfs_opendir(struct inode *, struct file *);
  43. static int nfs_closedir(struct inode *, struct file *);
  44. static int nfs_readdir(struct file *, void *, filldir_t);
  45. static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
  46. static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
  47. static int nfs_mkdir(struct inode *, struct dentry *, int);
  48. static int nfs_rmdir(struct inode *, struct dentry *);
  49. static int nfs_unlink(struct inode *, struct dentry *);
  50. static int nfs_symlink(struct inode *, struct dentry *, const char *);
  51. static int nfs_link(struct dentry *, struct inode *, struct dentry *);
  52. static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
  53. static int nfs_rename(struct inode *, struct dentry *,
  54. struct inode *, struct dentry *);
  55. static int nfs_fsync_dir(struct file *, int);
  56. static loff_t nfs_llseek_dir(struct file *, loff_t, int);
  57. static void nfs_readdir_clear_array(struct page*);
  58. const struct file_operations nfs_dir_operations = {
  59. .llseek = nfs_llseek_dir,
  60. .read = generic_read_dir,
  61. .readdir = nfs_readdir,
  62. .open = nfs_opendir,
  63. .release = nfs_closedir,
  64. .fsync = nfs_fsync_dir,
  65. };
  66. const struct inode_operations nfs_dir_inode_operations = {
  67. .create = nfs_create,
  68. .lookup = nfs_lookup,
  69. .link = nfs_link,
  70. .unlink = nfs_unlink,
  71. .symlink = nfs_symlink,
  72. .mkdir = nfs_mkdir,
  73. .rmdir = nfs_rmdir,
  74. .mknod = nfs_mknod,
  75. .rename = nfs_rename,
  76. .permission = nfs_permission,
  77. .getattr = nfs_getattr,
  78. .setattr = nfs_setattr,
  79. };
  80. const struct address_space_operations nfs_dir_aops = {
  81. .freepage = nfs_readdir_clear_array,
  82. };
  83. #ifdef CONFIG_NFS_V3
  84. const struct inode_operations nfs3_dir_inode_operations = {
  85. .create = nfs_create,
  86. .lookup = nfs_lookup,
  87. .link = nfs_link,
  88. .unlink = nfs_unlink,
  89. .symlink = nfs_symlink,
  90. .mkdir = nfs_mkdir,
  91. .rmdir = nfs_rmdir,
  92. .mknod = nfs_mknod,
  93. .rename = nfs_rename,
  94. .permission = nfs_permission,
  95. .getattr = nfs_getattr,
  96. .setattr = nfs_setattr,
  97. .listxattr = nfs3_listxattr,
  98. .getxattr = nfs3_getxattr,
  99. .setxattr = nfs3_setxattr,
  100. .removexattr = nfs3_removexattr,
  101. };
  102. #endif /* CONFIG_NFS_V3 */
  103. #ifdef CONFIG_NFS_V4
  104. static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
  105. static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd);
  106. const struct inode_operations nfs4_dir_inode_operations = {
  107. .create = nfs_open_create,
  108. .lookup = nfs_atomic_lookup,
  109. .link = nfs_link,
  110. .unlink = nfs_unlink,
  111. .symlink = nfs_symlink,
  112. .mkdir = nfs_mkdir,
  113. .rmdir = nfs_rmdir,
  114. .mknod = nfs_mknod,
  115. .rename = nfs_rename,
  116. .permission = nfs_permission,
  117. .getattr = nfs_getattr,
  118. .setattr = nfs_setattr,
  119. .getxattr = generic_getxattr,
  120. .setxattr = generic_setxattr,
  121. .listxattr = generic_listxattr,
  122. .removexattr = generic_removexattr,
  123. };
  124. #endif /* CONFIG_NFS_V4 */
  125. static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct rpc_cred *cred)
  126. {
  127. struct nfs_open_dir_context *ctx;
  128. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  129. if (ctx != NULL) {
  130. ctx->duped = 0;
  131. ctx->dir_cookie = 0;
  132. ctx->dup_cookie = 0;
  133. ctx->cred = get_rpccred(cred);
  134. } else
  135. ctx = ERR_PTR(-ENOMEM);
  136. return ctx;
  137. }
  138. static void put_nfs_open_dir_context(struct nfs_open_dir_context *ctx)
  139. {
  140. put_rpccred(ctx->cred);
  141. kfree(ctx);
  142. }
  143. /*
  144. * Open file
  145. */
  146. static int
  147. nfs_opendir(struct inode *inode, struct file *filp)
  148. {
  149. int res = 0;
  150. struct nfs_open_dir_context *ctx;
  151. struct rpc_cred *cred;
  152. dfprintk(FILE, "NFS: open dir(%s/%s)\n",
  153. filp->f_path.dentry->d_parent->d_name.name,
  154. filp->f_path.dentry->d_name.name);
  155. nfs_inc_stats(inode, NFSIOS_VFSOPEN);
  156. cred = rpc_lookup_cred();
  157. if (IS_ERR(cred))
  158. return PTR_ERR(cred);
  159. ctx = alloc_nfs_open_dir_context(cred);
  160. if (IS_ERR(ctx)) {
  161. res = PTR_ERR(ctx);
  162. goto out;
  163. }
  164. filp->private_data = ctx;
  165. if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
  166. /* This is a mountpoint, so d_revalidate will never
  167. * have been called, so we need to refresh the
  168. * inode (for close-open consistency) ourselves.
  169. */
  170. __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  171. }
  172. out:
  173. put_rpccred(cred);
  174. return res;
  175. }
  176. static int
  177. nfs_closedir(struct inode *inode, struct file *filp)
  178. {
  179. put_nfs_open_dir_context(filp->private_data);
  180. return 0;
  181. }
  182. struct nfs_cache_array_entry {
  183. u64 cookie;
  184. u64 ino;
  185. struct qstr string;
  186. unsigned char d_type;
  187. };
  188. struct nfs_cache_array {
  189. unsigned int size;
  190. int eof_index;
  191. u64 last_cookie;
  192. struct nfs_cache_array_entry array[0];
  193. };
  194. typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
  195. typedef struct {
  196. struct file *file;
  197. struct page *page;
  198. unsigned long page_index;
  199. u64 *dir_cookie;
  200. u64 last_cookie;
  201. loff_t current_index;
  202. decode_dirent_t decode;
  203. unsigned long timestamp;
  204. unsigned long gencount;
  205. unsigned int cache_entry_index;
  206. unsigned int plus:1;
  207. unsigned int eof:1;
  208. } nfs_readdir_descriptor_t;
  209. /*
  210. * The caller is responsible for calling nfs_readdir_release_array(page)
  211. */
  212. static
  213. struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
  214. {
  215. void *ptr;
  216. if (page == NULL)
  217. return ERR_PTR(-EIO);
  218. ptr = kmap(page);
  219. if (ptr == NULL)
  220. return ERR_PTR(-ENOMEM);
  221. return ptr;
  222. }
  223. static
  224. void nfs_readdir_release_array(struct page *page)
  225. {
  226. kunmap(page);
  227. }
  228. /*
  229. * we are freeing strings created by nfs_add_to_readdir_array()
  230. */
  231. static
  232. void nfs_readdir_clear_array(struct page *page)
  233. {
  234. struct nfs_cache_array *array;
  235. int i;
  236. array = kmap_atomic(page, KM_USER0);
  237. for (i = 0; i < array->size; i++)
  238. kfree(array->array[i].string.name);
  239. kunmap_atomic(array, KM_USER0);
  240. }
  241. /*
  242. * the caller is responsible for freeing qstr.name
  243. * when called by nfs_readdir_add_to_array, the strings will be freed in
  244. * nfs_clear_readdir_array()
  245. */
  246. static
  247. int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
  248. {
  249. string->len = len;
  250. string->name = kmemdup(name, len, GFP_KERNEL);
  251. if (string->name == NULL)
  252. return -ENOMEM;
  253. /*
  254. * Avoid a kmemleak false positive. The pointer to the name is stored
  255. * in a page cache page which kmemleak does not scan.
  256. */
  257. kmemleak_not_leak(string->name);
  258. string->hash = full_name_hash(name, len);
  259. return 0;
  260. }
  261. static
  262. int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
  263. {
  264. struct nfs_cache_array *array = nfs_readdir_get_array(page);
  265. struct nfs_cache_array_entry *cache_entry;
  266. int ret;
  267. if (IS_ERR(array))
  268. return PTR_ERR(array);
  269. cache_entry = &array->array[array->size];
  270. /* Check that this entry lies within the page bounds */
  271. ret = -ENOSPC;
  272. if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
  273. goto out;
  274. cache_entry->cookie = entry->prev_cookie;
  275. cache_entry->ino = entry->ino;
  276. cache_entry->d_type = entry->d_type;
  277. ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
  278. if (ret)
  279. goto out;
  280. array->last_cookie = entry->cookie;
  281. array->size++;
  282. if (entry->eof != 0)
  283. array->eof_index = array->size;
  284. out:
  285. nfs_readdir_release_array(page);
  286. return ret;
  287. }
  288. static
  289. int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
  290. {
  291. loff_t diff = desc->file->f_pos - desc->current_index;
  292. unsigned int index;
  293. struct nfs_open_dir_context *ctx = desc->file->private_data;
  294. if (diff < 0)
  295. goto out_eof;
  296. if (diff >= array->size) {
  297. if (array->eof_index >= 0)
  298. goto out_eof;
  299. return -EAGAIN;
  300. }
  301. index = (unsigned int)diff;
  302. *desc->dir_cookie = array->array[index].cookie;
  303. desc->cache_entry_index = index;
  304. ctx->duped = 0;
  305. return 0;
  306. out_eof:
  307. desc->eof = 1;
  308. return -EBADCOOKIE;
  309. }
  310. static
  311. int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
  312. {
  313. int i;
  314. loff_t new_pos;
  315. int status = -EAGAIN;
  316. struct nfs_open_dir_context *ctx = desc->file->private_data;
  317. for (i = 0; i < array->size; i++) {
  318. if (array->array[i].cookie == *desc->dir_cookie) {
  319. new_pos = desc->current_index + i;
  320. if (new_pos < desc->file->f_pos) {
  321. ctx->dup_cookie = *desc->dir_cookie;
  322. ctx->duped = 1;
  323. }
  324. desc->file->f_pos = new_pos;
  325. desc->cache_entry_index = i;
  326. return 0;
  327. }
  328. }
  329. if (array->eof_index >= 0) {
  330. status = -EBADCOOKIE;
  331. if (*desc->dir_cookie == array->last_cookie)
  332. desc->eof = 1;
  333. }
  334. return status;
  335. }
  336. static
  337. int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
  338. {
  339. struct nfs_cache_array *array;
  340. int status;
  341. array = nfs_readdir_get_array(desc->page);
  342. if (IS_ERR(array)) {
  343. status = PTR_ERR(array);
  344. goto out;
  345. }
  346. if (*desc->dir_cookie == 0)
  347. status = nfs_readdir_search_for_pos(array, desc);
  348. else
  349. status = nfs_readdir_search_for_cookie(array, desc);
  350. if (status == -EAGAIN) {
  351. desc->last_cookie = array->last_cookie;
  352. desc->current_index += array->size;
  353. desc->page_index++;
  354. }
  355. nfs_readdir_release_array(desc->page);
  356. out:
  357. return status;
  358. }
  359. /* Fill a page with xdr information before transferring to the cache page */
  360. static
  361. int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
  362. struct nfs_entry *entry, struct file *file, struct inode *inode)
  363. {
  364. struct nfs_open_dir_context *ctx = file->private_data;
  365. struct rpc_cred *cred = ctx->cred;
  366. unsigned long timestamp, gencount;
  367. int error;
  368. again:
  369. timestamp = jiffies;
  370. gencount = nfs_inc_attr_generation_counter();
  371. error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
  372. NFS_SERVER(inode)->dtsize, desc->plus);
  373. if (error < 0) {
  374. /* We requested READDIRPLUS, but the server doesn't grok it */
  375. if (error == -ENOTSUPP && desc->plus) {
  376. NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
  377. clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
  378. desc->plus = 0;
  379. goto again;
  380. }
  381. goto error;
  382. }
  383. desc->timestamp = timestamp;
  384. desc->gencount = gencount;
  385. error:
  386. return error;
  387. }
  388. static int xdr_decode(nfs_readdir_descriptor_t *desc,
  389. struct nfs_entry *entry, struct xdr_stream *xdr)
  390. {
  391. int error;
  392. error = desc->decode(xdr, entry, desc->plus);
  393. if (error)
  394. return error;
  395. entry->fattr->time_start = desc->timestamp;
  396. entry->fattr->gencount = desc->gencount;
  397. return 0;
  398. }
  399. static
  400. int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
  401. {
  402. if (dentry->d_inode == NULL)
  403. goto different;
  404. if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0)
  405. goto different;
  406. return 1;
  407. different:
  408. return 0;
  409. }
  410. static
  411. void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
  412. {
  413. struct qstr filename = {
  414. .len = entry->len,
  415. .name = entry->name,
  416. };
  417. struct dentry *dentry;
  418. struct dentry *alias;
  419. struct inode *dir = parent->d_inode;
  420. struct inode *inode;
  421. if (filename.name[0] == '.') {
  422. if (filename.len == 1)
  423. return;
  424. if (filename.len == 2 && filename.name[1] == '.')
  425. return;
  426. }
  427. filename.hash = full_name_hash(filename.name, filename.len);
  428. dentry = d_lookup(parent, &filename);
  429. if (dentry != NULL) {
  430. if (nfs_same_file(dentry, entry)) {
  431. nfs_refresh_inode(dentry->d_inode, entry->fattr);
  432. goto out;
  433. } else {
  434. d_drop(dentry);
  435. dput(dentry);
  436. }
  437. }
  438. dentry = d_alloc(parent, &filename);
  439. if (dentry == NULL)
  440. return;
  441. inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
  442. if (IS_ERR(inode))
  443. goto out;
  444. alias = d_materialise_unique(dentry, inode);
  445. if (IS_ERR(alias))
  446. goto out;
  447. else if (alias) {
  448. nfs_set_verifier(alias, nfs_save_change_attribute(dir));
  449. dput(alias);
  450. } else
  451. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  452. out:
  453. dput(dentry);
  454. }
  455. /* Perform conversion from xdr to cache array */
  456. static
  457. int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
  458. struct page **xdr_pages, struct page *page, unsigned int buflen)
  459. {
  460. struct xdr_stream stream;
  461. struct xdr_buf buf;
  462. struct page *scratch;
  463. struct nfs_cache_array *array;
  464. unsigned int count = 0;
  465. int status;
  466. scratch = alloc_page(GFP_KERNEL);
  467. if (scratch == NULL)
  468. return -ENOMEM;
  469. xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
  470. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  471. do {
  472. status = xdr_decode(desc, entry, &stream);
  473. if (status != 0) {
  474. if (status == -EAGAIN)
  475. status = 0;
  476. break;
  477. }
  478. count++;
  479. if (desc->plus != 0)
  480. nfs_prime_dcache(desc->file->f_path.dentry, entry);
  481. status = nfs_readdir_add_to_array(entry, page);
  482. if (status != 0)
  483. break;
  484. } while (!entry->eof);
  485. if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
  486. array = nfs_readdir_get_array(page);
  487. if (!IS_ERR(array)) {
  488. array->eof_index = array->size;
  489. status = 0;
  490. nfs_readdir_release_array(page);
  491. } else
  492. status = PTR_ERR(array);
  493. }
  494. put_page(scratch);
  495. return status;
  496. }
  497. static
  498. void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
  499. {
  500. unsigned int i;
  501. for (i = 0; i < npages; i++)
  502. put_page(pages[i]);
  503. }
  504. static
  505. void nfs_readdir_free_large_page(void *ptr, struct page **pages,
  506. unsigned int npages)
  507. {
  508. nfs_readdir_free_pagearray(pages, npages);
  509. }
  510. /*
  511. * nfs_readdir_large_page will allocate pages that must be freed with a call
  512. * to nfs_readdir_free_large_page
  513. */
  514. static
  515. int nfs_readdir_large_page(struct page **pages, unsigned int npages)
  516. {
  517. unsigned int i;
  518. for (i = 0; i < npages; i++) {
  519. struct page *page = alloc_page(GFP_KERNEL);
  520. if (page == NULL)
  521. goto out_freepages;
  522. pages[i] = page;
  523. }
  524. return 0;
  525. out_freepages:
  526. nfs_readdir_free_pagearray(pages, i);
  527. return -ENOMEM;
  528. }
  529. static
  530. int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
  531. {
  532. struct page *pages[NFS_MAX_READDIR_PAGES];
  533. void *pages_ptr = NULL;
  534. struct nfs_entry entry;
  535. struct file *file = desc->file;
  536. struct nfs_cache_array *array;
  537. int status = -ENOMEM;
  538. unsigned int array_size = ARRAY_SIZE(pages);
  539. entry.prev_cookie = 0;
  540. entry.cookie = desc->last_cookie;
  541. entry.eof = 0;
  542. entry.fh = nfs_alloc_fhandle();
  543. entry.fattr = nfs_alloc_fattr();
  544. entry.server = NFS_SERVER(inode);
  545. if (entry.fh == NULL || entry.fattr == NULL)
  546. goto out;
  547. array = nfs_readdir_get_array(page);
  548. if (IS_ERR(array)) {
  549. status = PTR_ERR(array);
  550. goto out;
  551. }
  552. memset(array, 0, sizeof(struct nfs_cache_array));
  553. array->eof_index = -1;
  554. status = nfs_readdir_large_page(pages, array_size);
  555. if (status < 0)
  556. goto out_release_array;
  557. do {
  558. unsigned int pglen;
  559. status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
  560. if (status < 0)
  561. break;
  562. pglen = status;
  563. status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
  564. if (status < 0) {
  565. if (status == -ENOSPC)
  566. status = 0;
  567. break;
  568. }
  569. } while (array->eof_index < 0);
  570. nfs_readdir_free_large_page(pages_ptr, pages, array_size);
  571. out_release_array:
  572. nfs_readdir_release_array(page);
  573. out:
  574. nfs_free_fattr(entry.fattr);
  575. nfs_free_fhandle(entry.fh);
  576. return status;
  577. }
  578. /*
  579. * Now we cache directories properly, by converting xdr information
  580. * to an array that can be used for lookups later. This results in
  581. * fewer cache pages, since we can store more information on each page.
  582. * We only need to convert from xdr once so future lookups are much simpler
  583. */
  584. static
  585. int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
  586. {
  587. struct inode *inode = desc->file->f_path.dentry->d_inode;
  588. int ret;
  589. ret = nfs_readdir_xdr_to_array(desc, page, inode);
  590. if (ret < 0)
  591. goto error;
  592. SetPageUptodate(page);
  593. if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
  594. /* Should never happen */
  595. nfs_zap_mapping(inode, inode->i_mapping);
  596. }
  597. unlock_page(page);
  598. return 0;
  599. error:
  600. unlock_page(page);
  601. return ret;
  602. }
  603. static
  604. void cache_page_release(nfs_readdir_descriptor_t *desc)
  605. {
  606. if (!desc->page->mapping)
  607. nfs_readdir_clear_array(desc->page);
  608. page_cache_release(desc->page);
  609. desc->page = NULL;
  610. }
  611. static
  612. struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
  613. {
  614. return read_cache_page(desc->file->f_path.dentry->d_inode->i_mapping,
  615. desc->page_index, (filler_t *)nfs_readdir_filler, desc);
  616. }
  617. /*
  618. * Returns 0 if desc->dir_cookie was found on page desc->page_index
  619. */
  620. static
  621. int find_cache_page(nfs_readdir_descriptor_t *desc)
  622. {
  623. int res;
  624. desc->page = get_cache_page(desc);
  625. if (IS_ERR(desc->page))
  626. return PTR_ERR(desc->page);
  627. res = nfs_readdir_search_array(desc);
  628. if (res != 0)
  629. cache_page_release(desc);
  630. return res;
  631. }
  632. /* Search for desc->dir_cookie from the beginning of the page cache */
  633. static inline
  634. int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
  635. {
  636. int res;
  637. if (desc->page_index == 0) {
  638. desc->current_index = 0;
  639. desc->last_cookie = 0;
  640. }
  641. do {
  642. res = find_cache_page(desc);
  643. } while (res == -EAGAIN);
  644. return res;
  645. }
  646. /*
  647. * Once we've found the start of the dirent within a page: fill 'er up...
  648. */
  649. static
  650. int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
  651. filldir_t filldir)
  652. {
  653. struct file *file = desc->file;
  654. int i = 0;
  655. int res = 0;
  656. struct nfs_cache_array *array = NULL;
  657. struct nfs_open_dir_context *ctx = file->private_data;
  658. if (ctx->duped != 0 && ctx->dup_cookie == *desc->dir_cookie) {
  659. if (printk_ratelimit()) {
  660. pr_notice("NFS: directory %s/%s contains a readdir loop. "
  661. "Please contact your server vendor. "
  662. "Offending cookie: %llu\n",
  663. file->f_dentry->d_parent->d_name.name,
  664. file->f_dentry->d_name.name,
  665. *desc->dir_cookie);
  666. }
  667. res = -ELOOP;
  668. goto out;
  669. }
  670. array = nfs_readdir_get_array(desc->page);
  671. if (IS_ERR(array)) {
  672. res = PTR_ERR(array);
  673. goto out;
  674. }
  675. for (i = desc->cache_entry_index; i < array->size; i++) {
  676. struct nfs_cache_array_entry *ent;
  677. ent = &array->array[i];
  678. if (filldir(dirent, ent->string.name, ent->string.len,
  679. file->f_pos, nfs_compat_user_ino64(ent->ino),
  680. ent->d_type) < 0) {
  681. desc->eof = 1;
  682. break;
  683. }
  684. file->f_pos++;
  685. if (i < (array->size-1))
  686. *desc->dir_cookie = array->array[i+1].cookie;
  687. else
  688. *desc->dir_cookie = array->last_cookie;
  689. }
  690. if (array->eof_index >= 0)
  691. desc->eof = 1;
  692. nfs_readdir_release_array(desc->page);
  693. out:
  694. cache_page_release(desc);
  695. dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
  696. (unsigned long long)*desc->dir_cookie, res);
  697. return res;
  698. }
  699. /*
  700. * If we cannot find a cookie in our cache, we suspect that this is
  701. * because it points to a deleted file, so we ask the server to return
  702. * whatever it thinks is the next entry. We then feed this to filldir.
  703. * If all goes well, we should then be able to find our way round the
  704. * cache on the next call to readdir_search_pagecache();
  705. *
  706. * NOTE: we cannot add the anonymous page to the pagecache because
  707. * the data it contains might not be page aligned. Besides,
  708. * we should already have a complete representation of the
  709. * directory in the page cache by the time we get here.
  710. */
  711. static inline
  712. int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
  713. filldir_t filldir)
  714. {
  715. struct page *page = NULL;
  716. int status;
  717. struct inode *inode = desc->file->f_path.dentry->d_inode;
  718. dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
  719. (unsigned long long)*desc->dir_cookie);
  720. page = alloc_page(GFP_HIGHUSER);
  721. if (!page) {
  722. status = -ENOMEM;
  723. goto out;
  724. }
  725. desc->page_index = 0;
  726. desc->last_cookie = *desc->dir_cookie;
  727. desc->page = page;
  728. status = nfs_readdir_xdr_to_array(desc, page, inode);
  729. if (status < 0)
  730. goto out_release;
  731. status = nfs_do_filldir(desc, dirent, filldir);
  732. out:
  733. dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
  734. __func__, status);
  735. return status;
  736. out_release:
  737. cache_page_release(desc);
  738. goto out;
  739. }
  740. /* The file offset position represents the dirent entry number. A
  741. last cookie cache takes care of the common case of reading the
  742. whole directory.
  743. */
  744. static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  745. {
  746. struct dentry *dentry = filp->f_path.dentry;
  747. struct inode *inode = dentry->d_inode;
  748. nfs_readdir_descriptor_t my_desc,
  749. *desc = &my_desc;
  750. struct nfs_open_dir_context *dir_ctx = filp->private_data;
  751. int res;
  752. dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
  753. dentry->d_parent->d_name.name, dentry->d_name.name,
  754. (long long)filp->f_pos);
  755. nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
  756. /*
  757. * filp->f_pos points to the dirent entry number.
  758. * *desc->dir_cookie has the cookie for the next entry. We have
  759. * to either find the entry with the appropriate number or
  760. * revalidate the cookie.
  761. */
  762. memset(desc, 0, sizeof(*desc));
  763. desc->file = filp;
  764. desc->dir_cookie = &dir_ctx->dir_cookie;
  765. desc->decode = NFS_PROTO(inode)->decode_dirent;
  766. desc->plus = NFS_USE_READDIRPLUS(inode);
  767. nfs_block_sillyrename(dentry);
  768. res = nfs_revalidate_mapping(inode, filp->f_mapping);
  769. if (res < 0)
  770. goto out;
  771. do {
  772. res = readdir_search_pagecache(desc);
  773. if (res == -EBADCOOKIE) {
  774. res = 0;
  775. /* This means either end of directory */
  776. if (*desc->dir_cookie && desc->eof == 0) {
  777. /* Or that the server has 'lost' a cookie */
  778. res = uncached_readdir(desc, dirent, filldir);
  779. if (res == 0)
  780. continue;
  781. }
  782. break;
  783. }
  784. if (res == -ETOOSMALL && desc->plus) {
  785. clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
  786. nfs_zap_caches(inode);
  787. desc->page_index = 0;
  788. desc->plus = 0;
  789. desc->eof = 0;
  790. continue;
  791. }
  792. if (res < 0)
  793. break;
  794. res = nfs_do_filldir(desc, dirent, filldir);
  795. if (res < 0)
  796. break;
  797. } while (!desc->eof);
  798. out:
  799. nfs_unblock_sillyrename(dentry);
  800. if (res > 0)
  801. res = 0;
  802. dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
  803. dentry->d_parent->d_name.name, dentry->d_name.name,
  804. res);
  805. return res;
  806. }
  807. static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
  808. {
  809. struct dentry *dentry = filp->f_path.dentry;
  810. struct inode *inode = dentry->d_inode;
  811. struct nfs_open_dir_context *dir_ctx = filp->private_data;
  812. dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
  813. dentry->d_parent->d_name.name,
  814. dentry->d_name.name,
  815. offset, origin);
  816. mutex_lock(&inode->i_mutex);
  817. switch (origin) {
  818. case 1:
  819. offset += filp->f_pos;
  820. case 0:
  821. if (offset >= 0)
  822. break;
  823. default:
  824. offset = -EINVAL;
  825. goto out;
  826. }
  827. if (offset != filp->f_pos) {
  828. filp->f_pos = offset;
  829. dir_ctx->dir_cookie = 0;
  830. dir_ctx->duped = 0;
  831. }
  832. out:
  833. mutex_unlock(&inode->i_mutex);
  834. return offset;
  835. }
  836. /*
  837. * All directory operations under NFS are synchronous, so fsync()
  838. * is a dummy operation.
  839. */
  840. static int nfs_fsync_dir(struct file *filp, int datasync)
  841. {
  842. struct dentry *dentry = filp->f_path.dentry;
  843. dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
  844. dentry->d_parent->d_name.name, dentry->d_name.name,
  845. datasync);
  846. nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
  847. return 0;
  848. }
  849. /**
  850. * nfs_force_lookup_revalidate - Mark the directory as having changed
  851. * @dir - pointer to directory inode
  852. *
  853. * This forces the revalidation code in nfs_lookup_revalidate() to do a
  854. * full lookup on all child dentries of 'dir' whenever a change occurs
  855. * on the server that might have invalidated our dcache.
  856. *
  857. * The caller should be holding dir->i_lock
  858. */
  859. void nfs_force_lookup_revalidate(struct inode *dir)
  860. {
  861. NFS_I(dir)->cache_change_attribute++;
  862. }
  863. /*
  864. * A check for whether or not the parent directory has changed.
  865. * In the case it has, we assume that the dentries are untrustworthy
  866. * and may need to be looked up again.
  867. */
  868. static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
  869. {
  870. if (IS_ROOT(dentry))
  871. return 1;
  872. if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
  873. return 0;
  874. if (!nfs_verify_change_attribute(dir, dentry->d_time))
  875. return 0;
  876. /* Revalidate nfsi->cache_change_attribute before we declare a match */
  877. if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
  878. return 0;
  879. if (!nfs_verify_change_attribute(dir, dentry->d_time))
  880. return 0;
  881. return 1;
  882. }
  883. /*
  884. * Return the intent data that applies to this particular path component
  885. *
  886. * Note that the current set of intents only apply to the very last
  887. * component of the path.
  888. * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
  889. */
  890. static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd,
  891. unsigned int mask)
  892. {
  893. if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
  894. return 0;
  895. return nd->flags & mask;
  896. }
  897. /*
  898. * Use intent information to check whether or not we're going to do
  899. * an O_EXCL create using this path component.
  900. */
  901. static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
  902. {
  903. if (NFS_PROTO(dir)->version == 2)
  904. return 0;
  905. return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
  906. }
  907. /*
  908. * Inode and filehandle revalidation for lookups.
  909. *
  910. * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
  911. * or if the intent information indicates that we're about to open this
  912. * particular file and the "nocto" mount flag is not set.
  913. *
  914. */
  915. static inline
  916. int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
  917. {
  918. struct nfs_server *server = NFS_SERVER(inode);
  919. if (IS_AUTOMOUNT(inode))
  920. return 0;
  921. if (nd != NULL) {
  922. /* VFS wants an on-the-wire revalidation */
  923. if (nd->flags & LOOKUP_REVAL)
  924. goto out_force;
  925. /* This is an open(2) */
  926. if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
  927. !(server->flags & NFS_MOUNT_NOCTO) &&
  928. (S_ISREG(inode->i_mode) ||
  929. S_ISDIR(inode->i_mode)))
  930. goto out_force;
  931. return 0;
  932. }
  933. return nfs_revalidate_inode(server, inode);
  934. out_force:
  935. return __nfs_revalidate_inode(server, inode);
  936. }
  937. /*
  938. * We judge how long we want to trust negative
  939. * dentries by looking at the parent inode mtime.
  940. *
  941. * If parent mtime has changed, we revalidate, else we wait for a
  942. * period corresponding to the parent's attribute cache timeout value.
  943. */
  944. static inline
  945. int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
  946. struct nameidata *nd)
  947. {
  948. /* Don't revalidate a negative dentry if we're creating a new file */
  949. if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
  950. return 0;
  951. if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
  952. return 1;
  953. return !nfs_check_verifier(dir, dentry);
  954. }
  955. /*
  956. * This is called every time the dcache has a lookup hit,
  957. * and we should check whether we can really trust that
  958. * lookup.
  959. *
  960. * NOTE! The hit can be a negative hit too, don't assume
  961. * we have an inode!
  962. *
  963. * If the parent directory is seen to have changed, we throw out the
  964. * cached dentry and do a new lookup.
  965. */
  966. static int nfs_lookup_revalidate(struct dentry *dentry, struct nameidata *nd)
  967. {
  968. struct inode *dir;
  969. struct inode *inode;
  970. struct dentry *parent;
  971. struct nfs_fh *fhandle = NULL;
  972. struct nfs_fattr *fattr = NULL;
  973. int error;
  974. if (nd->flags & LOOKUP_RCU)
  975. return -ECHILD;
  976. parent = dget_parent(dentry);
  977. dir = parent->d_inode;
  978. nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
  979. inode = dentry->d_inode;
  980. if (!inode) {
  981. if (nfs_neg_need_reval(dir, dentry, nd))
  982. goto out_bad;
  983. goto out_valid;
  984. }
  985. if (is_bad_inode(inode)) {
  986. dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
  987. __func__, dentry->d_parent->d_name.name,
  988. dentry->d_name.name);
  989. goto out_bad;
  990. }
  991. if (nfs_have_delegation(inode, FMODE_READ))
  992. goto out_set_verifier;
  993. /* Force a full look up iff the parent directory has changed */
  994. if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
  995. if (nfs_lookup_verify_inode(inode, nd))
  996. goto out_zap_parent;
  997. goto out_valid;
  998. }
  999. if (NFS_STALE(inode))
  1000. goto out_bad;
  1001. error = -ENOMEM;
  1002. fhandle = nfs_alloc_fhandle();
  1003. fattr = nfs_alloc_fattr();
  1004. if (fhandle == NULL || fattr == NULL)
  1005. goto out_error;
  1006. error = NFS_PROTO(dir)->lookup(NFS_SERVER(dir)->client, dir, &dentry->d_name, fhandle, fattr);
  1007. if (error)
  1008. goto out_bad;
  1009. if (nfs_compare_fh(NFS_FH(inode), fhandle))
  1010. goto out_bad;
  1011. if ((error = nfs_refresh_inode(inode, fattr)) != 0)
  1012. goto out_bad;
  1013. nfs_free_fattr(fattr);
  1014. nfs_free_fhandle(fhandle);
  1015. out_set_verifier:
  1016. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1017. out_valid:
  1018. dput(parent);
  1019. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
  1020. __func__, dentry->d_parent->d_name.name,
  1021. dentry->d_name.name);
  1022. return 1;
  1023. out_zap_parent:
  1024. nfs_zap_caches(dir);
  1025. out_bad:
  1026. nfs_mark_for_revalidate(dir);
  1027. if (inode && S_ISDIR(inode->i_mode)) {
  1028. /* Purge readdir caches. */
  1029. nfs_zap_caches(inode);
  1030. /* If we have submounts, don't unhash ! */
  1031. if (have_submounts(dentry))
  1032. goto out_valid;
  1033. if (dentry->d_flags & DCACHE_DISCONNECTED)
  1034. goto out_valid;
  1035. shrink_dcache_parent(dentry);
  1036. }
  1037. d_drop(dentry);
  1038. nfs_free_fattr(fattr);
  1039. nfs_free_fhandle(fhandle);
  1040. dput(parent);
  1041. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
  1042. __func__, dentry->d_parent->d_name.name,
  1043. dentry->d_name.name);
  1044. return 0;
  1045. out_error:
  1046. nfs_free_fattr(fattr);
  1047. nfs_free_fhandle(fhandle);
  1048. dput(parent);
  1049. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
  1050. __func__, dentry->d_parent->d_name.name,
  1051. dentry->d_name.name, error);
  1052. return error;
  1053. }
  1054. /*
  1055. * This is called from dput() when d_count is going to 0.
  1056. */
  1057. static int nfs_dentry_delete(const struct dentry *dentry)
  1058. {
  1059. dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
  1060. dentry->d_parent->d_name.name, dentry->d_name.name,
  1061. dentry->d_flags);
  1062. /* Unhash any dentry with a stale inode */
  1063. if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
  1064. return 1;
  1065. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1066. /* Unhash it, so that ->d_iput() would be called */
  1067. return 1;
  1068. }
  1069. if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
  1070. /* Unhash it, so that ancestors of killed async unlink
  1071. * files will be cleaned up during umount */
  1072. return 1;
  1073. }
  1074. return 0;
  1075. }
  1076. static void nfs_drop_nlink(struct inode *inode)
  1077. {
  1078. spin_lock(&inode->i_lock);
  1079. if (inode->i_nlink > 0)
  1080. drop_nlink(inode);
  1081. spin_unlock(&inode->i_lock);
  1082. }
  1083. /*
  1084. * Called when the dentry loses inode.
  1085. * We use it to clean up silly-renamed files.
  1086. */
  1087. static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
  1088. {
  1089. if (S_ISDIR(inode->i_mode))
  1090. /* drop any readdir cache as it could easily be old */
  1091. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
  1092. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1093. drop_nlink(inode);
  1094. nfs_complete_unlink(dentry, inode);
  1095. }
  1096. iput(inode);
  1097. }
  1098. static void nfs_d_release(struct dentry *dentry)
  1099. {
  1100. /* free cached devname value, if it survived that far */
  1101. if (unlikely(dentry->d_fsdata)) {
  1102. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1103. WARN_ON(1);
  1104. else
  1105. kfree(dentry->d_fsdata);
  1106. }
  1107. }
  1108. const struct dentry_operations nfs_dentry_operations = {
  1109. .d_revalidate = nfs_lookup_revalidate,
  1110. .d_delete = nfs_dentry_delete,
  1111. .d_iput = nfs_dentry_iput,
  1112. .d_automount = nfs_d_automount,
  1113. .d_release = nfs_d_release,
  1114. };
  1115. static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  1116. {
  1117. struct dentry *res;
  1118. struct dentry *parent;
  1119. struct inode *inode = NULL;
  1120. struct nfs_fh *fhandle = NULL;
  1121. struct nfs_fattr *fattr = NULL;
  1122. int error;
  1123. dfprintk(VFS, "NFS: lookup(%s/%s)\n",
  1124. dentry->d_parent->d_name.name, dentry->d_name.name);
  1125. nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
  1126. res = ERR_PTR(-ENAMETOOLONG);
  1127. if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
  1128. goto out;
  1129. /*
  1130. * If we're doing an exclusive create, optimize away the lookup
  1131. * but don't hash the dentry.
  1132. */
  1133. if (nfs_is_exclusive_create(dir, nd)) {
  1134. d_instantiate(dentry, NULL);
  1135. res = NULL;
  1136. goto out;
  1137. }
  1138. res = ERR_PTR(-ENOMEM);
  1139. fhandle = nfs_alloc_fhandle();
  1140. fattr = nfs_alloc_fattr();
  1141. if (fhandle == NULL || fattr == NULL)
  1142. goto out;
  1143. parent = dentry->d_parent;
  1144. /* Protect against concurrent sillydeletes */
  1145. nfs_block_sillyrename(parent);
  1146. error = NFS_PROTO(dir)->lookup(NFS_SERVER(dir)->client, dir, &dentry->d_name, fhandle, fattr);
  1147. if (error == -ENOENT)
  1148. goto no_entry;
  1149. if (error < 0) {
  1150. res = ERR_PTR(error);
  1151. goto out_unblock_sillyrename;
  1152. }
  1153. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  1154. res = ERR_CAST(inode);
  1155. if (IS_ERR(res))
  1156. goto out_unblock_sillyrename;
  1157. no_entry:
  1158. res = d_materialise_unique(dentry, inode);
  1159. if (res != NULL) {
  1160. if (IS_ERR(res))
  1161. goto out_unblock_sillyrename;
  1162. dentry = res;
  1163. }
  1164. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1165. out_unblock_sillyrename:
  1166. nfs_unblock_sillyrename(parent);
  1167. out:
  1168. nfs_free_fattr(fattr);
  1169. nfs_free_fhandle(fhandle);
  1170. return res;
  1171. }
  1172. #ifdef CONFIG_NFS_V4
  1173. static int nfs_open_revalidate(struct dentry *, struct nameidata *);
  1174. const struct dentry_operations nfs4_dentry_operations = {
  1175. .d_revalidate = nfs_open_revalidate,
  1176. .d_delete = nfs_dentry_delete,
  1177. .d_iput = nfs_dentry_iput,
  1178. .d_automount = nfs_d_automount,
  1179. .d_release = nfs_d_release,
  1180. };
  1181. /*
  1182. * Use intent information to determine whether we need to substitute
  1183. * the NFSv4-style stateful OPEN for the LOOKUP call
  1184. */
  1185. static int is_atomic_open(struct nameidata *nd)
  1186. {
  1187. if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
  1188. return 0;
  1189. /* NFS does not (yet) have a stateful open for directories */
  1190. if (nd->flags & LOOKUP_DIRECTORY)
  1191. return 0;
  1192. /* Are we trying to write to a read only partition? */
  1193. if (__mnt_is_readonly(nd->path.mnt) &&
  1194. (nd->intent.open.flags & (O_CREAT|O_TRUNC|O_ACCMODE)))
  1195. return 0;
  1196. return 1;
  1197. }
  1198. static fmode_t flags_to_mode(int flags)
  1199. {
  1200. fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
  1201. if ((flags & O_ACCMODE) != O_WRONLY)
  1202. res |= FMODE_READ;
  1203. if ((flags & O_ACCMODE) != O_RDONLY)
  1204. res |= FMODE_WRITE;
  1205. return res;
  1206. }
  1207. static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags)
  1208. {
  1209. struct nfs_open_context *ctx;
  1210. struct rpc_cred *cred;
  1211. fmode_t fmode = flags_to_mode(open_flags);
  1212. cred = rpc_lookup_cred();
  1213. if (IS_ERR(cred))
  1214. return ERR_CAST(cred);
  1215. ctx = alloc_nfs_open_context(dentry, cred, fmode);
  1216. put_rpccred(cred);
  1217. if (ctx == NULL)
  1218. return ERR_PTR(-ENOMEM);
  1219. return ctx;
  1220. }
  1221. static int do_open(struct inode *inode, struct file *filp)
  1222. {
  1223. nfs_fscache_set_inode_cookie(inode, filp);
  1224. return 0;
  1225. }
  1226. static int nfs_intent_set_file(struct nameidata *nd, struct nfs_open_context *ctx)
  1227. {
  1228. struct file *filp;
  1229. int ret = 0;
  1230. /* If the open_intent is for execute, we have an extra check to make */
  1231. if (ctx->mode & FMODE_EXEC) {
  1232. ret = nfs_may_open(ctx->dentry->d_inode,
  1233. ctx->cred,
  1234. nd->intent.open.flags);
  1235. if (ret < 0)
  1236. goto out;
  1237. }
  1238. filp = lookup_instantiate_filp(nd, ctx->dentry, do_open);
  1239. if (IS_ERR(filp))
  1240. ret = PTR_ERR(filp);
  1241. else
  1242. nfs_file_set_open_context(filp, ctx);
  1243. out:
  1244. put_nfs_open_context(ctx);
  1245. return ret;
  1246. }
  1247. static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  1248. {
  1249. struct nfs_open_context *ctx;
  1250. struct iattr attr;
  1251. struct dentry *res = NULL;
  1252. struct inode *inode;
  1253. int open_flags;
  1254. int err;
  1255. dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
  1256. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1257. /* Check that we are indeed trying to open this file */
  1258. if (!is_atomic_open(nd))
  1259. goto no_open;
  1260. if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
  1261. res = ERR_PTR(-ENAMETOOLONG);
  1262. goto out;
  1263. }
  1264. /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
  1265. * the dentry. */
  1266. if (nd->flags & LOOKUP_EXCL) {
  1267. d_instantiate(dentry, NULL);
  1268. goto out;
  1269. }
  1270. open_flags = nd->intent.open.flags;
  1271. ctx = create_nfs_open_context(dentry, open_flags);
  1272. res = ERR_CAST(ctx);
  1273. if (IS_ERR(ctx))
  1274. goto out;
  1275. if (nd->flags & LOOKUP_CREATE) {
  1276. attr.ia_mode = nd->intent.open.create_mode;
  1277. attr.ia_valid = ATTR_MODE;
  1278. attr.ia_mode &= ~current_umask();
  1279. } else {
  1280. open_flags &= ~(O_EXCL | O_CREAT);
  1281. attr.ia_valid = 0;
  1282. }
  1283. /* Open the file on the server */
  1284. nfs_block_sillyrename(dentry->d_parent);
  1285. inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
  1286. if (IS_ERR(inode)) {
  1287. nfs_unblock_sillyrename(dentry->d_parent);
  1288. put_nfs_open_context(ctx);
  1289. switch (PTR_ERR(inode)) {
  1290. /* Make a negative dentry */
  1291. case -ENOENT:
  1292. d_add(dentry, NULL);
  1293. res = NULL;
  1294. goto out;
  1295. /* This turned out not to be a regular file */
  1296. case -ENOTDIR:
  1297. goto no_open;
  1298. case -ELOOP:
  1299. if (!(nd->intent.open.flags & O_NOFOLLOW))
  1300. goto no_open;
  1301. /* case -EISDIR: */
  1302. /* case -EINVAL: */
  1303. default:
  1304. res = ERR_CAST(inode);
  1305. goto out;
  1306. }
  1307. }
  1308. res = d_add_unique(dentry, inode);
  1309. nfs_unblock_sillyrename(dentry->d_parent);
  1310. if (res != NULL) {
  1311. dput(ctx->dentry);
  1312. ctx->dentry = dget(res);
  1313. dentry = res;
  1314. }
  1315. err = nfs_intent_set_file(nd, ctx);
  1316. if (err < 0) {
  1317. if (res != NULL)
  1318. dput(res);
  1319. return ERR_PTR(err);
  1320. }
  1321. out:
  1322. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1323. return res;
  1324. no_open:
  1325. return nfs_lookup(dir, dentry, nd);
  1326. }
  1327. static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
  1328. {
  1329. struct dentry *parent = NULL;
  1330. struct inode *inode;
  1331. struct inode *dir;
  1332. struct nfs_open_context *ctx;
  1333. int openflags, ret = 0;
  1334. if (nd->flags & LOOKUP_RCU)
  1335. return -ECHILD;
  1336. inode = dentry->d_inode;
  1337. if (!is_atomic_open(nd) || d_mountpoint(dentry))
  1338. goto no_open;
  1339. parent = dget_parent(dentry);
  1340. dir = parent->d_inode;
  1341. /* We can't create new files in nfs_open_revalidate(), so we
  1342. * optimize away revalidation of negative dentries.
  1343. */
  1344. if (inode == NULL) {
  1345. if (!nfs_neg_need_reval(dir, dentry, nd))
  1346. ret = 1;
  1347. goto out;
  1348. }
  1349. /* NFS only supports OPEN on regular files */
  1350. if (!S_ISREG(inode->i_mode))
  1351. goto no_open_dput;
  1352. openflags = nd->intent.open.flags;
  1353. /* We cannot do exclusive creation on a positive dentry */
  1354. if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  1355. goto no_open_dput;
  1356. /* We can't create new files, or truncate existing ones here */
  1357. openflags &= ~(O_CREAT|O_EXCL|O_TRUNC);
  1358. ctx = create_nfs_open_context(dentry, openflags);
  1359. ret = PTR_ERR(ctx);
  1360. if (IS_ERR(ctx))
  1361. goto out;
  1362. /*
  1363. * Note: we're not holding inode->i_mutex and so may be racing with
  1364. * operations that change the directory. We therefore save the
  1365. * change attribute *before* we do the RPC call.
  1366. */
  1367. inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, NULL);
  1368. if (IS_ERR(inode)) {
  1369. ret = PTR_ERR(inode);
  1370. switch (ret) {
  1371. case -EPERM:
  1372. case -EACCES:
  1373. case -EDQUOT:
  1374. case -ENOSPC:
  1375. case -EROFS:
  1376. goto out_put_ctx;
  1377. default:
  1378. goto out_drop;
  1379. }
  1380. }
  1381. iput(inode);
  1382. if (inode != dentry->d_inode)
  1383. goto out_drop;
  1384. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1385. ret = nfs_intent_set_file(nd, ctx);
  1386. if (ret >= 0)
  1387. ret = 1;
  1388. out:
  1389. dput(parent);
  1390. return ret;
  1391. out_drop:
  1392. d_drop(dentry);
  1393. ret = 0;
  1394. out_put_ctx:
  1395. put_nfs_open_context(ctx);
  1396. goto out;
  1397. no_open_dput:
  1398. dput(parent);
  1399. no_open:
  1400. return nfs_lookup_revalidate(dentry, nd);
  1401. }
  1402. static int nfs_open_create(struct inode *dir, struct dentry *dentry, int mode,
  1403. struct nameidata *nd)
  1404. {
  1405. struct nfs_open_context *ctx = NULL;
  1406. struct iattr attr;
  1407. int error;
  1408. int open_flags = O_CREAT|O_EXCL;
  1409. dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
  1410. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1411. attr.ia_mode = mode;
  1412. attr.ia_valid = ATTR_MODE;
  1413. if (nd)
  1414. open_flags = nd->intent.open.flags;
  1415. ctx = create_nfs_open_context(dentry, open_flags);
  1416. error = PTR_ERR(ctx);
  1417. if (IS_ERR(ctx))
  1418. goto out_err_drop;
  1419. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, ctx);
  1420. if (error != 0)
  1421. goto out_put_ctx;
  1422. if (nd) {
  1423. error = nfs_intent_set_file(nd, ctx);
  1424. if (error < 0)
  1425. goto out_err;
  1426. } else {
  1427. put_nfs_open_context(ctx);
  1428. }
  1429. return 0;
  1430. out_put_ctx:
  1431. put_nfs_open_context(ctx);
  1432. out_err_drop:
  1433. d_drop(dentry);
  1434. out_err:
  1435. return error;
  1436. }
  1437. #endif /* CONFIG_NFSV4 */
  1438. /*
  1439. * Code common to create, mkdir, and mknod.
  1440. */
  1441. int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
  1442. struct nfs_fattr *fattr)
  1443. {
  1444. struct dentry *parent = dget_parent(dentry);
  1445. struct inode *dir = parent->d_inode;
  1446. struct inode *inode;
  1447. int error = -EACCES;
  1448. d_drop(dentry);
  1449. /* We may have been initialized further down */
  1450. if (dentry->d_inode)
  1451. goto out;
  1452. if (fhandle->size == 0) {
  1453. error = NFS_PROTO(dir)->lookup(NFS_SERVER(dir)->client, dir, &dentry->d_name, fhandle, fattr);
  1454. if (error)
  1455. goto out_error;
  1456. }
  1457. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1458. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  1459. struct nfs_server *server = NFS_SB(dentry->d_sb);
  1460. error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
  1461. if (error < 0)
  1462. goto out_error;
  1463. }
  1464. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  1465. error = PTR_ERR(inode);
  1466. if (IS_ERR(inode))
  1467. goto out_error;
  1468. d_add(dentry, inode);
  1469. out:
  1470. dput(parent);
  1471. return 0;
  1472. out_error:
  1473. nfs_mark_for_revalidate(dir);
  1474. dput(parent);
  1475. return error;
  1476. }
  1477. /*
  1478. * Following a failed create operation, we drop the dentry rather
  1479. * than retain a negative dentry. This avoids a problem in the event
  1480. * that the operation succeeded on the server, but an error in the
  1481. * reply path made it appear to have failed.
  1482. */
  1483. static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1484. struct nameidata *nd)
  1485. {
  1486. struct iattr attr;
  1487. int error;
  1488. int open_flags = O_CREAT|O_EXCL;
  1489. dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
  1490. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1491. attr.ia_mode = mode;
  1492. attr.ia_valid = ATTR_MODE;
  1493. if (nd)
  1494. open_flags = nd->intent.open.flags;
  1495. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, NULL);
  1496. if (error != 0)
  1497. goto out_err;
  1498. return 0;
  1499. out_err:
  1500. d_drop(dentry);
  1501. return error;
  1502. }
  1503. /*
  1504. * See comments for nfs_proc_create regarding failed operations.
  1505. */
  1506. static int
  1507. nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
  1508. {
  1509. struct iattr attr;
  1510. int status;
  1511. dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
  1512. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1513. if (!new_valid_dev(rdev))
  1514. return -EINVAL;
  1515. attr.ia_mode = mode;
  1516. attr.ia_valid = ATTR_MODE;
  1517. status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
  1518. if (status != 0)
  1519. goto out_err;
  1520. return 0;
  1521. out_err:
  1522. d_drop(dentry);
  1523. return status;
  1524. }
  1525. /*
  1526. * See comments for nfs_proc_create regarding failed operations.
  1527. */
  1528. static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1529. {
  1530. struct iattr attr;
  1531. int error;
  1532. dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
  1533. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1534. attr.ia_valid = ATTR_MODE;
  1535. attr.ia_mode = mode | S_IFDIR;
  1536. error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
  1537. if (error != 0)
  1538. goto out_err;
  1539. return 0;
  1540. out_err:
  1541. d_drop(dentry);
  1542. return error;
  1543. }
  1544. static void nfs_dentry_handle_enoent(struct dentry *dentry)
  1545. {
  1546. if (dentry->d_inode != NULL && !d_unhashed(dentry))
  1547. d_delete(dentry);
  1548. }
  1549. static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
  1550. {
  1551. int error;
  1552. dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
  1553. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1554. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  1555. /* Ensure the VFS deletes this inode */
  1556. if (error == 0 && dentry->d_inode != NULL)
  1557. clear_nlink(dentry->d_inode);
  1558. else if (error == -ENOENT)
  1559. nfs_dentry_handle_enoent(dentry);
  1560. return error;
  1561. }
  1562. /*
  1563. * Remove a file after making sure there are no pending writes,
  1564. * and after checking that the file has only one user.
  1565. *
  1566. * We invalidate the attribute cache and free the inode prior to the operation
  1567. * to avoid possible races if the server reuses the inode.
  1568. */
  1569. static int nfs_safe_remove(struct dentry *dentry)
  1570. {
  1571. struct inode *dir = dentry->d_parent->d_inode;
  1572. struct inode *inode = dentry->d_inode;
  1573. int error = -EBUSY;
  1574. dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
  1575. dentry->d_parent->d_name.name, dentry->d_name.name);
  1576. /* If the dentry was sillyrenamed, we simply call d_delete() */
  1577. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1578. error = 0;
  1579. goto out;
  1580. }
  1581. if (inode != NULL) {
  1582. nfs_inode_return_delegation(inode);
  1583. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1584. /* The VFS may want to delete this inode */
  1585. if (error == 0)
  1586. nfs_drop_nlink(inode);
  1587. nfs_mark_for_revalidate(inode);
  1588. } else
  1589. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1590. if (error == -ENOENT)
  1591. nfs_dentry_handle_enoent(dentry);
  1592. out:
  1593. return error;
  1594. }
  1595. /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
  1596. * belongs to an active ".nfs..." file and we return -EBUSY.
  1597. *
  1598. * If sillyrename() returns 0, we do nothing, otherwise we unlink.
  1599. */
  1600. static int nfs_unlink(struct inode *dir, struct dentry *dentry)
  1601. {
  1602. int error;
  1603. int need_rehash = 0;
  1604. dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
  1605. dir->i_ino, dentry->d_name.name);
  1606. spin_lock(&dentry->d_lock);
  1607. if (dentry->d_count > 1) {
  1608. spin_unlock(&dentry->d_lock);
  1609. /* Start asynchronous writeout of the inode */
  1610. write_inode_now(dentry->d_inode, 0);
  1611. error = nfs_sillyrename(dir, dentry);
  1612. return error;
  1613. }
  1614. if (!d_unhashed(dentry)) {
  1615. __d_drop(dentry);
  1616. need_rehash = 1;
  1617. }
  1618. spin_unlock(&dentry->d_lock);
  1619. error = nfs_safe_remove(dentry);
  1620. if (!error || error == -ENOENT) {
  1621. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1622. } else if (need_rehash)
  1623. d_rehash(dentry);
  1624. return error;
  1625. }
  1626. /*
  1627. * To create a symbolic link, most file systems instantiate a new inode,
  1628. * add a page to it containing the path, then write it out to the disk
  1629. * using prepare_write/commit_write.
  1630. *
  1631. * Unfortunately the NFS client can't create the in-core inode first
  1632. * because it needs a file handle to create an in-core inode (see
  1633. * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
  1634. * symlink request has completed on the server.
  1635. *
  1636. * So instead we allocate a raw page, copy the symname into it, then do
  1637. * the SYMLINK request with the page as the buffer. If it succeeds, we
  1638. * now have a new file handle and can instantiate an in-core NFS inode
  1639. * and move the raw page into its mapping.
  1640. */
  1641. static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1642. {
  1643. struct pagevec lru_pvec;
  1644. struct page *page;
  1645. char *kaddr;
  1646. struct iattr attr;
  1647. unsigned int pathlen = strlen(symname);
  1648. int error;
  1649. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
  1650. dir->i_ino, dentry->d_name.name, symname);
  1651. if (pathlen > PAGE_SIZE)
  1652. return -ENAMETOOLONG;
  1653. attr.ia_mode = S_IFLNK | S_IRWXUGO;
  1654. attr.ia_valid = ATTR_MODE;
  1655. page = alloc_page(GFP_HIGHUSER);
  1656. if (!page)
  1657. return -ENOMEM;
  1658. kaddr = kmap_atomic(page, KM_USER0);
  1659. memcpy(kaddr, symname, pathlen);
  1660. if (pathlen < PAGE_SIZE)
  1661. memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
  1662. kunmap_atomic(kaddr, KM_USER0);
  1663. error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
  1664. if (error != 0) {
  1665. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
  1666. dir->i_sb->s_id, dir->i_ino,
  1667. dentry->d_name.name, symname, error);
  1668. d_drop(dentry);
  1669. __free_page(page);
  1670. return error;
  1671. }
  1672. /*
  1673. * No big deal if we can't add this page to the page cache here.
  1674. * READLINK will get the missing page from the server if needed.
  1675. */
  1676. pagevec_init(&lru_pvec, 0);
  1677. if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
  1678. GFP_KERNEL)) {
  1679. pagevec_add(&lru_pvec, page);
  1680. pagevec_lru_add_file(&lru_pvec);
  1681. SetPageUptodate(page);
  1682. unlock_page(page);
  1683. } else
  1684. __free_page(page);
  1685. return 0;
  1686. }
  1687. static int
  1688. nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1689. {
  1690. struct inode *inode = old_dentry->d_inode;
  1691. int error;
  1692. dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
  1693. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1694. dentry->d_parent->d_name.name, dentry->d_name.name);
  1695. nfs_inode_return_delegation(inode);
  1696. d_drop(dentry);
  1697. error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
  1698. if (error == 0) {
  1699. ihold(inode);
  1700. d_add(dentry, inode);
  1701. }
  1702. return error;
  1703. }
  1704. /*
  1705. * RENAME
  1706. * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
  1707. * different file handle for the same inode after a rename (e.g. when
  1708. * moving to a different directory). A fail-safe method to do so would
  1709. * be to look up old_dir/old_name, create a link to new_dir/new_name and
  1710. * rename the old file using the sillyrename stuff. This way, the original
  1711. * file in old_dir will go away when the last process iput()s the inode.
  1712. *
  1713. * FIXED.
  1714. *
  1715. * It actually works quite well. One needs to have the possibility for
  1716. * at least one ".nfs..." file in each directory the file ever gets
  1717. * moved or linked to which happens automagically with the new
  1718. * implementation that only depends on the dcache stuff instead of
  1719. * using the inode layer
  1720. *
  1721. * Unfortunately, things are a little more complicated than indicated
  1722. * above. For a cross-directory move, we want to make sure we can get
  1723. * rid of the old inode after the operation. This means there must be
  1724. * no pending writes (if it's a file), and the use count must be 1.
  1725. * If these conditions are met, we can drop the dentries before doing
  1726. * the rename.
  1727. */
  1728. static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1729. struct inode *new_dir, struct dentry *new_dentry)
  1730. {
  1731. struct inode *old_inode = old_dentry->d_inode;
  1732. struct inode *new_inode = new_dentry->d_inode;
  1733. struct dentry *dentry = NULL, *rehash = NULL;
  1734. int error = -EBUSY;
  1735. dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
  1736. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1737. new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
  1738. new_dentry->d_count);
  1739. /*
  1740. * For non-directories, check whether the target is busy and if so,
  1741. * make a copy of the dentry and then do a silly-rename. If the
  1742. * silly-rename succeeds, the copied dentry is hashed and becomes
  1743. * the new target.
  1744. */
  1745. if (new_inode && !S_ISDIR(new_inode->i_mode)) {
  1746. /*
  1747. * To prevent any new references to the target during the
  1748. * rename, we unhash the dentry in advance.
  1749. */
  1750. if (!d_unhashed(new_dentry)) {
  1751. d_drop(new_dentry);
  1752. rehash = new_dentry;
  1753. }
  1754. if (new_dentry->d_count > 2) {
  1755. int err;
  1756. /* copy the target dentry's name */
  1757. dentry = d_alloc(new_dentry->d_parent,
  1758. &new_dentry->d_name);
  1759. if (!dentry)
  1760. goto out;
  1761. /* silly-rename the existing target ... */
  1762. err = nfs_sillyrename(new_dir, new_dentry);
  1763. if (err)
  1764. goto out;
  1765. new_dentry = dentry;
  1766. rehash = NULL;
  1767. new_inode = NULL;
  1768. }
  1769. }
  1770. nfs_inode_return_delegation(old_inode);
  1771. if (new_inode != NULL)
  1772. nfs_inode_return_delegation(new_inode);
  1773. error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
  1774. new_dir, &new_dentry->d_name);
  1775. nfs_mark_for_revalidate(old_inode);
  1776. out:
  1777. if (rehash)
  1778. d_rehash(rehash);
  1779. if (!error) {
  1780. if (new_inode != NULL)
  1781. nfs_drop_nlink(new_inode);
  1782. d_move(old_dentry, new_dentry);
  1783. nfs_set_verifier(new_dentry,
  1784. nfs_save_change_attribute(new_dir));
  1785. } else if (error == -ENOENT)
  1786. nfs_dentry_handle_enoent(old_dentry);
  1787. /* new dentry created? */
  1788. if (dentry)
  1789. dput(dentry);
  1790. return error;
  1791. }
  1792. static DEFINE_SPINLOCK(nfs_access_lru_lock);
  1793. static LIST_HEAD(nfs_access_lru_list);
  1794. static atomic_long_t nfs_access_nr_entries;
  1795. static void nfs_access_free_entry(struct nfs_access_entry *entry)
  1796. {
  1797. put_rpccred(entry->cred);
  1798. kfree(entry);
  1799. smp_mb__before_atomic_dec();
  1800. atomic_long_dec(&nfs_access_nr_entries);
  1801. smp_mb__after_atomic_dec();
  1802. }
  1803. static void nfs_access_free_list(struct list_head *head)
  1804. {
  1805. struct nfs_access_entry *cache;
  1806. while (!list_empty(head)) {
  1807. cache = list_entry(head->next, struct nfs_access_entry, lru);
  1808. list_del(&cache->lru);
  1809. nfs_access_free_entry(cache);
  1810. }
  1811. }
  1812. int nfs_access_cache_shrinker(struct shrinker *shrink,
  1813. struct shrink_control *sc)
  1814. {
  1815. LIST_HEAD(head);
  1816. struct nfs_inode *nfsi, *next;
  1817. struct nfs_access_entry *cache;
  1818. int nr_to_scan = sc->nr_to_scan;
  1819. gfp_t gfp_mask = sc->gfp_mask;
  1820. if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
  1821. return (nr_to_scan == 0) ? 0 : -1;
  1822. spin_lock(&nfs_access_lru_lock);
  1823. list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
  1824. struct inode *inode;
  1825. if (nr_to_scan-- == 0)
  1826. break;
  1827. inode = &nfsi->vfs_inode;
  1828. spin_lock(&inode->i_lock);
  1829. if (list_empty(&nfsi->access_cache_entry_lru))
  1830. goto remove_lru_entry;
  1831. cache = list_entry(nfsi->access_cache_entry_lru.next,
  1832. struct nfs_access_entry, lru);
  1833. list_move(&cache->lru, &head);
  1834. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1835. if (!list_empty(&nfsi->access_cache_entry_lru))
  1836. list_move_tail(&nfsi->access_cache_inode_lru,
  1837. &nfs_access_lru_list);
  1838. else {
  1839. remove_lru_entry:
  1840. list_del_init(&nfsi->access_cache_inode_lru);
  1841. smp_mb__before_clear_bit();
  1842. clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
  1843. smp_mb__after_clear_bit();
  1844. }
  1845. spin_unlock(&inode->i_lock);
  1846. }
  1847. spin_unlock(&nfs_access_lru_lock);
  1848. nfs_access_free_list(&head);
  1849. return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
  1850. }
  1851. static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
  1852. {
  1853. struct rb_root *root_node = &nfsi->access_cache;
  1854. struct rb_node *n;
  1855. struct nfs_access_entry *entry;
  1856. /* Unhook entries from the cache */
  1857. while ((n = rb_first(root_node)) != NULL) {
  1858. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1859. rb_erase(n, root_node);
  1860. list_move(&entry->lru, head);
  1861. }
  1862. nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
  1863. }
  1864. void nfs_access_zap_cache(struct inode *inode)
  1865. {
  1866. LIST_HEAD(head);
  1867. if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
  1868. return;
  1869. /* Remove from global LRU init */
  1870. spin_lock(&nfs_access_lru_lock);
  1871. if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  1872. list_del_init(&NFS_I(inode)->access_cache_inode_lru);
  1873. spin_lock(&inode->i_lock);
  1874. __nfs_access_zap_cache(NFS_I(inode), &head);
  1875. spin_unlock(&inode->i_lock);
  1876. spin_unlock(&nfs_access_lru_lock);
  1877. nfs_access_free_list(&head);
  1878. }
  1879. static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
  1880. {
  1881. struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
  1882. struct nfs_access_entry *entry;
  1883. while (n != NULL) {
  1884. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1885. if (cred < entry->cred)
  1886. n = n->rb_left;
  1887. else if (cred > entry->cred)
  1888. n = n->rb_right;
  1889. else
  1890. return entry;
  1891. }
  1892. return NULL;
  1893. }
  1894. static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
  1895. {
  1896. struct nfs_inode *nfsi = NFS_I(inode);
  1897. struct nfs_access_entry *cache;
  1898. int err = -ENOENT;
  1899. spin_lock(&inode->i_lock);
  1900. if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
  1901. goto out_zap;
  1902. cache = nfs_access_search_rbtree(inode, cred);
  1903. if (cache == NULL)
  1904. goto out;
  1905. if (!nfs_have_delegated_attributes(inode) &&
  1906. !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
  1907. goto out_stale;
  1908. res->jiffies = cache->jiffies;
  1909. res->cred = cache->cred;
  1910. res->mask = cache->mask;
  1911. list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
  1912. err = 0;
  1913. out:
  1914. spin_unlock(&inode->i_lock);
  1915. return err;
  1916. out_stale:
  1917. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1918. list_del(&cache->lru);
  1919. spin_unlock(&inode->i_lock);
  1920. nfs_access_free_entry(cache);
  1921. return -ENOENT;
  1922. out_zap:
  1923. spin_unlock(&inode->i_lock);
  1924. nfs_access_zap_cache(inode);
  1925. return -ENOENT;
  1926. }
  1927. static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
  1928. {
  1929. struct nfs_inode *nfsi = NFS_I(inode);
  1930. struct rb_root *root_node = &nfsi->access_cache;
  1931. struct rb_node **p = &root_node->rb_node;
  1932. struct rb_node *parent = NULL;
  1933. struct nfs_access_entry *entry;
  1934. spin_lock(&inode->i_lock);
  1935. while (*p != NULL) {
  1936. parent = *p;
  1937. entry = rb_entry(parent, struct nfs_access_entry, rb_node);
  1938. if (set->cred < entry->cred)
  1939. p = &parent->rb_left;
  1940. else if (set->cred > entry->cred)
  1941. p = &parent->rb_right;
  1942. else
  1943. goto found;
  1944. }
  1945. rb_link_node(&set->rb_node, parent, p);
  1946. rb_insert_color(&set->rb_node, root_node);
  1947. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1948. spin_unlock(&inode->i_lock);
  1949. return;
  1950. found:
  1951. rb_replace_node(parent, &set->rb_node, root_node);
  1952. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1953. list_del(&entry->lru);
  1954. spin_unlock(&inode->i_lock);
  1955. nfs_access_free_entry(entry);
  1956. }
  1957. static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
  1958. {
  1959. struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
  1960. if (cache == NULL)
  1961. return;
  1962. RB_CLEAR_NODE(&cache->rb_node);
  1963. cache->jiffies = set->jiffies;
  1964. cache->cred = get_rpccred(set->cred);
  1965. cache->mask = set->mask;
  1966. nfs_access_add_rbtree(inode, cache);
  1967. /* Update accounting */
  1968. smp_mb__before_atomic_inc();
  1969. atomic_long_inc(&nfs_access_nr_entries);
  1970. smp_mb__after_atomic_inc();
  1971. /* Add inode to global LRU list */
  1972. if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
  1973. spin_lock(&nfs_access_lru_lock);
  1974. if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  1975. list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
  1976. &nfs_access_lru_list);
  1977. spin_unlock(&nfs_access_lru_lock);
  1978. }
  1979. }
  1980. static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
  1981. {
  1982. struct nfs_access_entry cache;
  1983. int status;
  1984. status = nfs_access_get_cached(inode, cred, &cache);
  1985. if (status == 0)
  1986. goto out;
  1987. /* Be clever: ask server to check for all possible rights */
  1988. cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
  1989. cache.cred = cred;
  1990. cache.jiffies = jiffies;
  1991. status = NFS_PROTO(inode)->access(inode, &cache);
  1992. if (status != 0) {
  1993. if (status == -ESTALE) {
  1994. nfs_zap_caches(inode);
  1995. if (!S_ISDIR(inode->i_mode))
  1996. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  1997. }
  1998. return status;
  1999. }
  2000. nfs_access_add_cache(inode, &cache);
  2001. out:
  2002. if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  2003. return 0;
  2004. return -EACCES;
  2005. }
  2006. static int nfs_open_permission_mask(int openflags)
  2007. {
  2008. int mask = 0;
  2009. if ((openflags & O_ACCMODE) != O_WRONLY)
  2010. mask |= MAY_READ;
  2011. if ((openflags & O_ACCMODE) != O_RDONLY)
  2012. mask |= MAY_WRITE;
  2013. if (openflags & __FMODE_EXEC)
  2014. mask |= MAY_EXEC;
  2015. return mask;
  2016. }
  2017. int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
  2018. {
  2019. return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
  2020. }
  2021. int nfs_permission(struct inode *inode, int mask)
  2022. {
  2023. struct rpc_cred *cred;
  2024. int res = 0;
  2025. if (mask & MAY_NOT_BLOCK)
  2026. return -ECHILD;
  2027. nfs_inc_stats(inode, NFSIOS_VFSACCESS);
  2028. if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  2029. goto out;
  2030. /* Is this sys_access() ? */
  2031. if (mask & (MAY_ACCESS | MAY_CHDIR))
  2032. goto force_lookup;
  2033. switch (inode->i_mode & S_IFMT) {
  2034. case S_IFLNK:
  2035. goto out;
  2036. case S_IFREG:
  2037. /* NFSv4 has atomic_open... */
  2038. if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
  2039. && (mask & MAY_OPEN)
  2040. && !(mask & MAY_EXEC))
  2041. goto out;
  2042. break;
  2043. case S_IFDIR:
  2044. /*
  2045. * Optimize away all write operations, since the server
  2046. * will check permissions when we perform the op.
  2047. */
  2048. if ((mask & MAY_WRITE) && !(mask & MAY_READ))
  2049. goto out;
  2050. }
  2051. force_lookup:
  2052. if (!NFS_PROTO(inode)->access)
  2053. goto out_notsup;
  2054. cred = rpc_lookup_cred();
  2055. if (!IS_ERR(cred)) {
  2056. res = nfs_do_access(inode, cred, mask);
  2057. put_rpccred(cred);
  2058. } else
  2059. res = PTR_ERR(cred);
  2060. out:
  2061. if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
  2062. res = -EACCES;
  2063. dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
  2064. inode->i_sb->s_id, inode->i_ino, mask, res);
  2065. return res;
  2066. out_notsup:
  2067. res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  2068. if (res == 0)
  2069. res = generic_permission(inode, mask);
  2070. goto out;
  2071. }
  2072. /*
  2073. * Local variables:
  2074. * version-control: t
  2075. * kept-new-versions: 5
  2076. * End:
  2077. */