namei.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773
  1. /*
  2. * linux/fs/namei.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * Some corrections by tytso.
  8. */
  9. /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
  10. * lookup logic.
  11. */
  12. /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/fs.h>
  18. #include <linux/namei.h>
  19. #include <linux/quotaops.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/fsnotify.h>
  22. #include <linux/smp_lock.h>
  23. #include <linux/personality.h>
  24. #include <linux/security.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/mount.h>
  27. #include <linux/audit.h>
  28. #include <linux/capability.h>
  29. #include <linux/file.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/namei.h>
  32. #include <asm/namei.h>
  33. #include <asm/uaccess.h>
  34. #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
  35. /* [Feb-1997 T. Schoebel-Theuer]
  36. * Fundamental changes in the pathname lookup mechanisms (namei)
  37. * were necessary because of omirr. The reason is that omirr needs
  38. * to know the _real_ pathname, not the user-supplied one, in case
  39. * of symlinks (and also when transname replacements occur).
  40. *
  41. * The new code replaces the old recursive symlink resolution with
  42. * an iterative one (in case of non-nested symlink chains). It does
  43. * this with calls to <fs>_follow_link().
  44. * As a side effect, dir_namei(), _namei() and follow_link() are now
  45. * replaced with a single function lookup_dentry() that can handle all
  46. * the special cases of the former code.
  47. *
  48. * With the new dcache, the pathname is stored at each inode, at least as
  49. * long as the refcount of the inode is positive. As a side effect, the
  50. * size of the dcache depends on the inode cache and thus is dynamic.
  51. *
  52. * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  53. * resolution to correspond with current state of the code.
  54. *
  55. * Note that the symlink resolution is not *completely* iterative.
  56. * There is still a significant amount of tail- and mid- recursion in
  57. * the algorithm. Also, note that <fs>_readlink() is not used in
  58. * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  59. * may return different results than <fs>_follow_link(). Many virtual
  60. * filesystems (including /proc) exhibit this behavior.
  61. */
  62. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  63. * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  64. * and the name already exists in form of a symlink, try to create the new
  65. * name indicated by the symlink. The old code always complained that the
  66. * name already exists, due to not following the symlink even if its target
  67. * is nonexistent. The new semantics affects also mknod() and link() when
  68. * the name is a symlink pointing to a non-existant name.
  69. *
  70. * I don't know which semantics is the right one, since I have no access
  71. * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  72. * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  73. * "old" one. Personally, I think the new semantics is much more logical.
  74. * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  75. * file does succeed in both HP-UX and SunOs, but not in Solaris
  76. * and in the old Linux semantics.
  77. */
  78. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  79. * semantics. See the comments in "open_namei" and "do_link" below.
  80. *
  81. * [10-Sep-98 Alan Modra] Another symlink change.
  82. */
  83. /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  84. * inside the path - always follow.
  85. * in the last component in creation/removal/renaming - never follow.
  86. * if LOOKUP_FOLLOW passed - follow.
  87. * if the pathname has trailing slashes - follow.
  88. * otherwise - don't follow.
  89. * (applied in that order).
  90. *
  91. * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  92. * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  93. * During the 2.4 we need to fix the userland stuff depending on it -
  94. * hopefully we will be able to get rid of that wart in 2.5. So far only
  95. * XEmacs seems to be relying on it...
  96. */
  97. /*
  98. * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
  99. * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
  100. * any extra contention...
  101. */
  102. /* In order to reduce some races, while at the same time doing additional
  103. * checking and hopefully speeding things up, we copy filenames to the
  104. * kernel data space before using them..
  105. *
  106. * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
  107. * PATH_MAX includes the nul terminator --RR.
  108. */
  109. static int do_getname(const char __user *filename, char *page)
  110. {
  111. int retval;
  112. unsigned long len = PATH_MAX;
  113. if (!segment_eq(get_fs(), KERNEL_DS)) {
  114. if ((unsigned long) filename >= TASK_SIZE)
  115. return -EFAULT;
  116. if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
  117. len = TASK_SIZE - (unsigned long) filename;
  118. }
  119. retval = strncpy_from_user(page, filename, len);
  120. if (retval > 0) {
  121. if (retval < len)
  122. return 0;
  123. return -ENAMETOOLONG;
  124. } else if (!retval)
  125. retval = -ENOENT;
  126. return retval;
  127. }
  128. char * getname(const char __user * filename)
  129. {
  130. char *tmp, *result;
  131. result = ERR_PTR(-ENOMEM);
  132. tmp = __getname();
  133. if (tmp) {
  134. int retval = do_getname(filename, tmp);
  135. result = tmp;
  136. if (retval < 0) {
  137. __putname(tmp);
  138. result = ERR_PTR(retval);
  139. }
  140. }
  141. audit_getname(result);
  142. return result;
  143. }
  144. #ifdef CONFIG_AUDITSYSCALL
  145. void putname(const char *name)
  146. {
  147. if (unlikely(!audit_dummy_context()))
  148. audit_putname(name);
  149. else
  150. __putname(name);
  151. }
  152. EXPORT_SYMBOL(putname);
  153. #endif
  154. /**
  155. * generic_permission - check for access rights on a Posix-like filesystem
  156. * @inode: inode to check access rights for
  157. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  158. * @check_acl: optional callback to check for Posix ACLs
  159. *
  160. * Used to check for read/write/execute permissions on a file.
  161. * We use "fsuid" for this, letting us set arbitrary permissions
  162. * for filesystem access without changing the "normal" uids which
  163. * are used for other things..
  164. */
  165. int generic_permission(struct inode *inode, int mask,
  166. int (*check_acl)(struct inode *inode, int mask))
  167. {
  168. umode_t mode = inode->i_mode;
  169. if (current->fsuid == inode->i_uid)
  170. mode >>= 6;
  171. else {
  172. if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
  173. int error = check_acl(inode, mask);
  174. if (error == -EACCES)
  175. goto check_capabilities;
  176. else if (error != -EAGAIN)
  177. return error;
  178. }
  179. if (in_group_p(inode->i_gid))
  180. mode >>= 3;
  181. }
  182. /*
  183. * If the DACs are ok we don't need any capability check.
  184. */
  185. if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
  186. return 0;
  187. check_capabilities:
  188. /*
  189. * Read/write DACs are always overridable.
  190. * Executable DACs are overridable if at least one exec bit is set.
  191. */
  192. if (!(mask & MAY_EXEC) ||
  193. (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
  194. if (capable(CAP_DAC_OVERRIDE))
  195. return 0;
  196. /*
  197. * Searching includes executable on directories, else just read.
  198. */
  199. if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
  200. if (capable(CAP_DAC_READ_SEARCH))
  201. return 0;
  202. return -EACCES;
  203. }
  204. int permission(struct inode *inode, int mask, struct nameidata *nd)
  205. {
  206. umode_t mode = inode->i_mode;
  207. int retval, submask;
  208. if (mask & MAY_WRITE) {
  209. /*
  210. * Nobody gets write access to a read-only fs.
  211. */
  212. if (IS_RDONLY(inode) &&
  213. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  214. return -EROFS;
  215. /*
  216. * Nobody gets write access to an immutable file.
  217. */
  218. if (IS_IMMUTABLE(inode))
  219. return -EACCES;
  220. }
  221. /*
  222. * MAY_EXEC on regular files requires special handling: We override
  223. * filesystem execute permissions if the mode bits aren't set.
  224. */
  225. if ((mask & MAY_EXEC) && S_ISREG(mode) && !(mode & S_IXUGO))
  226. return -EACCES;
  227. /* Ordinary permission routines do not understand MAY_APPEND. */
  228. submask = mask & ~MAY_APPEND;
  229. if (inode->i_op && inode->i_op->permission)
  230. retval = inode->i_op->permission(inode, submask, nd);
  231. else
  232. retval = generic_permission(inode, submask, NULL);
  233. if (retval)
  234. return retval;
  235. return security_inode_permission(inode, mask, nd);
  236. }
  237. /**
  238. * vfs_permission - check for access rights to a given path
  239. * @nd: lookup result that describes the path
  240. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  241. *
  242. * Used to check for read/write/execute permissions on a path.
  243. * We use "fsuid" for this, letting us set arbitrary permissions
  244. * for filesystem access without changing the "normal" uids which
  245. * are used for other things.
  246. */
  247. int vfs_permission(struct nameidata *nd, int mask)
  248. {
  249. return permission(nd->dentry->d_inode, mask, nd);
  250. }
  251. /**
  252. * file_permission - check for additional access rights to a given file
  253. * @file: file to check access rights for
  254. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  255. *
  256. * Used to check for read/write/execute permissions on an already opened
  257. * file.
  258. *
  259. * Note:
  260. * Do not use this function in new code. All access checks should
  261. * be done using vfs_permission().
  262. */
  263. int file_permission(struct file *file, int mask)
  264. {
  265. return permission(file->f_dentry->d_inode, mask, NULL);
  266. }
  267. /*
  268. * get_write_access() gets write permission for a file.
  269. * put_write_access() releases this write permission.
  270. * This is used for regular files.
  271. * We cannot support write (and maybe mmap read-write shared) accesses and
  272. * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
  273. * can have the following values:
  274. * 0: no writers, no VM_DENYWRITE mappings
  275. * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
  276. * > 0: (i_writecount) users are writing to the file.
  277. *
  278. * Normally we operate on that counter with atomic_{inc,dec} and it's safe
  279. * except for the cases where we don't hold i_writecount yet. Then we need to
  280. * use {get,deny}_write_access() - these functions check the sign and refuse
  281. * to do the change if sign is wrong. Exclusion between them is provided by
  282. * the inode->i_lock spinlock.
  283. */
  284. int get_write_access(struct inode * inode)
  285. {
  286. spin_lock(&inode->i_lock);
  287. if (atomic_read(&inode->i_writecount) < 0) {
  288. spin_unlock(&inode->i_lock);
  289. return -ETXTBSY;
  290. }
  291. atomic_inc(&inode->i_writecount);
  292. spin_unlock(&inode->i_lock);
  293. return 0;
  294. }
  295. int deny_write_access(struct file * file)
  296. {
  297. struct inode *inode = file->f_dentry->d_inode;
  298. spin_lock(&inode->i_lock);
  299. if (atomic_read(&inode->i_writecount) > 0) {
  300. spin_unlock(&inode->i_lock);
  301. return -ETXTBSY;
  302. }
  303. atomic_dec(&inode->i_writecount);
  304. spin_unlock(&inode->i_lock);
  305. return 0;
  306. }
  307. void path_release(struct nameidata *nd)
  308. {
  309. dput(nd->dentry);
  310. mntput(nd->mnt);
  311. }
  312. /*
  313. * umount() mustn't call path_release()/mntput() as that would clear
  314. * mnt_expiry_mark
  315. */
  316. void path_release_on_umount(struct nameidata *nd)
  317. {
  318. dput(nd->dentry);
  319. mntput_no_expire(nd->mnt);
  320. }
  321. /**
  322. * release_open_intent - free up open intent resources
  323. * @nd: pointer to nameidata
  324. */
  325. void release_open_intent(struct nameidata *nd)
  326. {
  327. if (nd->intent.open.file->f_dentry == NULL)
  328. put_filp(nd->intent.open.file);
  329. else
  330. fput(nd->intent.open.file);
  331. }
  332. static inline struct dentry *
  333. do_revalidate(struct dentry *dentry, struct nameidata *nd)
  334. {
  335. int status = dentry->d_op->d_revalidate(dentry, nd);
  336. if (unlikely(status <= 0)) {
  337. /*
  338. * The dentry failed validation.
  339. * If d_revalidate returned 0 attempt to invalidate
  340. * the dentry otherwise d_revalidate is asking us
  341. * to return a fail status.
  342. */
  343. if (!status) {
  344. if (!d_invalidate(dentry)) {
  345. dput(dentry);
  346. dentry = NULL;
  347. }
  348. } else {
  349. dput(dentry);
  350. dentry = ERR_PTR(status);
  351. }
  352. }
  353. return dentry;
  354. }
  355. /*
  356. * Internal lookup() using the new generic dcache.
  357. * SMP-safe
  358. */
  359. static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
  360. {
  361. struct dentry * dentry = __d_lookup(parent, name);
  362. /* lockess __d_lookup may fail due to concurrent d_move()
  363. * in some unrelated directory, so try with d_lookup
  364. */
  365. if (!dentry)
  366. dentry = d_lookup(parent, name);
  367. if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
  368. dentry = do_revalidate(dentry, nd);
  369. return dentry;
  370. }
  371. /*
  372. * Short-cut version of permission(), for calling by
  373. * path_walk(), when dcache lock is held. Combines parts
  374. * of permission() and generic_permission(), and tests ONLY for
  375. * MAY_EXEC permission.
  376. *
  377. * If appropriate, check DAC only. If not appropriate, or
  378. * short-cut DAC fails, then call permission() to do more
  379. * complete permission check.
  380. */
  381. static int exec_permission_lite(struct inode *inode,
  382. struct nameidata *nd)
  383. {
  384. umode_t mode = inode->i_mode;
  385. if (inode->i_op && inode->i_op->permission)
  386. return -EAGAIN;
  387. if (current->fsuid == inode->i_uid)
  388. mode >>= 6;
  389. else if (in_group_p(inode->i_gid))
  390. mode >>= 3;
  391. if (mode & MAY_EXEC)
  392. goto ok;
  393. if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
  394. goto ok;
  395. if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
  396. goto ok;
  397. if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
  398. goto ok;
  399. return -EACCES;
  400. ok:
  401. return security_inode_permission(inode, MAY_EXEC, nd);
  402. }
  403. /*
  404. * This is called when everything else fails, and we actually have
  405. * to go to the low-level filesystem to find out what we should do..
  406. *
  407. * We get the directory semaphore, and after getting that we also
  408. * make sure that nobody added the entry to the dcache in the meantime..
  409. * SMP-safe
  410. */
  411. static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
  412. {
  413. struct dentry * result;
  414. struct inode *dir = parent->d_inode;
  415. mutex_lock(&dir->i_mutex);
  416. /*
  417. * First re-do the cached lookup just in case it was created
  418. * while we waited for the directory semaphore..
  419. *
  420. * FIXME! This could use version numbering or similar to
  421. * avoid unnecessary cache lookups.
  422. *
  423. * The "dcache_lock" is purely to protect the RCU list walker
  424. * from concurrent renames at this point (we mustn't get false
  425. * negatives from the RCU list walk here, unlike the optimistic
  426. * fast walk).
  427. *
  428. * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
  429. */
  430. result = d_lookup(parent, name);
  431. if (!result) {
  432. struct dentry * dentry = d_alloc(parent, name);
  433. result = ERR_PTR(-ENOMEM);
  434. if (dentry) {
  435. result = dir->i_op->lookup(dir, dentry, nd);
  436. if (result)
  437. dput(dentry);
  438. else
  439. result = dentry;
  440. }
  441. mutex_unlock(&dir->i_mutex);
  442. return result;
  443. }
  444. /*
  445. * Uhhuh! Nasty case: the cache was re-populated while
  446. * we waited on the semaphore. Need to revalidate.
  447. */
  448. mutex_unlock(&dir->i_mutex);
  449. if (result->d_op && result->d_op->d_revalidate) {
  450. result = do_revalidate(result, nd);
  451. if (!result)
  452. result = ERR_PTR(-ENOENT);
  453. }
  454. return result;
  455. }
  456. static int __emul_lookup_dentry(const char *, struct nameidata *);
  457. /* SMP-safe */
  458. static __always_inline int
  459. walk_init_root(const char *name, struct nameidata *nd)
  460. {
  461. struct fs_struct *fs = current->fs;
  462. read_lock(&fs->lock);
  463. if (fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
  464. nd->mnt = mntget(fs->altrootmnt);
  465. nd->dentry = dget(fs->altroot);
  466. read_unlock(&fs->lock);
  467. if (__emul_lookup_dentry(name,nd))
  468. return 0;
  469. read_lock(&fs->lock);
  470. }
  471. nd->mnt = mntget(fs->rootmnt);
  472. nd->dentry = dget(fs->root);
  473. read_unlock(&fs->lock);
  474. return 1;
  475. }
  476. static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
  477. {
  478. int res = 0;
  479. char *name;
  480. if (IS_ERR(link))
  481. goto fail;
  482. if (*link == '/') {
  483. path_release(nd);
  484. if (!walk_init_root(link, nd))
  485. /* weird __emul_prefix() stuff did it */
  486. goto out;
  487. }
  488. res = link_path_walk(link, nd);
  489. out:
  490. if (nd->depth || res || nd->last_type!=LAST_NORM)
  491. return res;
  492. /*
  493. * If it is an iterative symlinks resolution in open_namei() we
  494. * have to copy the last component. And all that crap because of
  495. * bloody create() on broken symlinks. Furrfu...
  496. */
  497. name = __getname();
  498. if (unlikely(!name)) {
  499. path_release(nd);
  500. return -ENOMEM;
  501. }
  502. strcpy(name, nd->last.name);
  503. nd->last.name = name;
  504. return 0;
  505. fail:
  506. path_release(nd);
  507. return PTR_ERR(link);
  508. }
  509. struct path {
  510. struct vfsmount *mnt;
  511. struct dentry *dentry;
  512. };
  513. static inline void dput_path(struct path *path, struct nameidata *nd)
  514. {
  515. dput(path->dentry);
  516. if (path->mnt != nd->mnt)
  517. mntput(path->mnt);
  518. }
  519. static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
  520. {
  521. dput(nd->dentry);
  522. if (nd->mnt != path->mnt)
  523. mntput(nd->mnt);
  524. nd->mnt = path->mnt;
  525. nd->dentry = path->dentry;
  526. }
  527. static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
  528. {
  529. int error;
  530. void *cookie;
  531. struct dentry *dentry = path->dentry;
  532. touch_atime(path->mnt, dentry);
  533. nd_set_link(nd, NULL);
  534. if (path->mnt != nd->mnt) {
  535. path_to_nameidata(path, nd);
  536. dget(dentry);
  537. }
  538. mntget(path->mnt);
  539. cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
  540. error = PTR_ERR(cookie);
  541. if (!IS_ERR(cookie)) {
  542. char *s = nd_get_link(nd);
  543. error = 0;
  544. if (s)
  545. error = __vfs_follow_link(nd, s);
  546. if (dentry->d_inode->i_op->put_link)
  547. dentry->d_inode->i_op->put_link(dentry, nd, cookie);
  548. }
  549. dput(dentry);
  550. mntput(path->mnt);
  551. return error;
  552. }
  553. /*
  554. * This limits recursive symlink follows to 8, while
  555. * limiting consecutive symlinks to 40.
  556. *
  557. * Without that kind of total limit, nasty chains of consecutive
  558. * symlinks can cause almost arbitrarily long lookups.
  559. */
  560. static inline int do_follow_link(struct path *path, struct nameidata *nd)
  561. {
  562. int err = -ELOOP;
  563. if (current->link_count >= MAX_NESTED_LINKS)
  564. goto loop;
  565. if (current->total_link_count >= 40)
  566. goto loop;
  567. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  568. cond_resched();
  569. err = security_inode_follow_link(path->dentry, nd);
  570. if (err)
  571. goto loop;
  572. current->link_count++;
  573. current->total_link_count++;
  574. nd->depth++;
  575. err = __do_follow_link(path, nd);
  576. current->link_count--;
  577. nd->depth--;
  578. return err;
  579. loop:
  580. dput_path(path, nd);
  581. path_release(nd);
  582. return err;
  583. }
  584. int follow_up(struct vfsmount **mnt, struct dentry **dentry)
  585. {
  586. struct vfsmount *parent;
  587. struct dentry *mountpoint;
  588. spin_lock(&vfsmount_lock);
  589. parent=(*mnt)->mnt_parent;
  590. if (parent == *mnt) {
  591. spin_unlock(&vfsmount_lock);
  592. return 0;
  593. }
  594. mntget(parent);
  595. mountpoint=dget((*mnt)->mnt_mountpoint);
  596. spin_unlock(&vfsmount_lock);
  597. dput(*dentry);
  598. *dentry = mountpoint;
  599. mntput(*mnt);
  600. *mnt = parent;
  601. return 1;
  602. }
  603. /* no need for dcache_lock, as serialization is taken care in
  604. * namespace.c
  605. */
  606. static int __follow_mount(struct path *path)
  607. {
  608. int res = 0;
  609. while (d_mountpoint(path->dentry)) {
  610. struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
  611. if (!mounted)
  612. break;
  613. dput(path->dentry);
  614. if (res)
  615. mntput(path->mnt);
  616. path->mnt = mounted;
  617. path->dentry = dget(mounted->mnt_root);
  618. res = 1;
  619. }
  620. return res;
  621. }
  622. static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
  623. {
  624. while (d_mountpoint(*dentry)) {
  625. struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
  626. if (!mounted)
  627. break;
  628. dput(*dentry);
  629. mntput(*mnt);
  630. *mnt = mounted;
  631. *dentry = dget(mounted->mnt_root);
  632. }
  633. }
  634. /* no need for dcache_lock, as serialization is taken care in
  635. * namespace.c
  636. */
  637. int follow_down(struct vfsmount **mnt, struct dentry **dentry)
  638. {
  639. struct vfsmount *mounted;
  640. mounted = lookup_mnt(*mnt, *dentry);
  641. if (mounted) {
  642. dput(*dentry);
  643. mntput(*mnt);
  644. *mnt = mounted;
  645. *dentry = dget(mounted->mnt_root);
  646. return 1;
  647. }
  648. return 0;
  649. }
  650. static __always_inline void follow_dotdot(struct nameidata *nd)
  651. {
  652. struct fs_struct *fs = current->fs;
  653. while(1) {
  654. struct vfsmount *parent;
  655. struct dentry *old = nd->dentry;
  656. read_lock(&fs->lock);
  657. if (nd->dentry == fs->root &&
  658. nd->mnt == fs->rootmnt) {
  659. read_unlock(&fs->lock);
  660. break;
  661. }
  662. read_unlock(&fs->lock);
  663. spin_lock(&dcache_lock);
  664. if (nd->dentry != nd->mnt->mnt_root) {
  665. nd->dentry = dget(nd->dentry->d_parent);
  666. spin_unlock(&dcache_lock);
  667. dput(old);
  668. break;
  669. }
  670. spin_unlock(&dcache_lock);
  671. spin_lock(&vfsmount_lock);
  672. parent = nd->mnt->mnt_parent;
  673. if (parent == nd->mnt) {
  674. spin_unlock(&vfsmount_lock);
  675. break;
  676. }
  677. mntget(parent);
  678. nd->dentry = dget(nd->mnt->mnt_mountpoint);
  679. spin_unlock(&vfsmount_lock);
  680. dput(old);
  681. mntput(nd->mnt);
  682. nd->mnt = parent;
  683. }
  684. follow_mount(&nd->mnt, &nd->dentry);
  685. }
  686. /*
  687. * It's more convoluted than I'd like it to be, but... it's still fairly
  688. * small and for now I'd prefer to have fast path as straight as possible.
  689. * It _is_ time-critical.
  690. */
  691. static int do_lookup(struct nameidata *nd, struct qstr *name,
  692. struct path *path)
  693. {
  694. struct vfsmount *mnt = nd->mnt;
  695. struct dentry *dentry = __d_lookup(nd->dentry, name);
  696. if (!dentry)
  697. goto need_lookup;
  698. if (dentry->d_op && dentry->d_op->d_revalidate)
  699. goto need_revalidate;
  700. done:
  701. path->mnt = mnt;
  702. path->dentry = dentry;
  703. __follow_mount(path);
  704. return 0;
  705. need_lookup:
  706. dentry = real_lookup(nd->dentry, name, nd);
  707. if (IS_ERR(dentry))
  708. goto fail;
  709. goto done;
  710. need_revalidate:
  711. dentry = do_revalidate(dentry, nd);
  712. if (!dentry)
  713. goto need_lookup;
  714. if (IS_ERR(dentry))
  715. goto fail;
  716. goto done;
  717. fail:
  718. return PTR_ERR(dentry);
  719. }
  720. /*
  721. * Name resolution.
  722. * This is the basic name resolution function, turning a pathname into
  723. * the final dentry. We expect 'base' to be positive and a directory.
  724. *
  725. * Returns 0 and nd will have valid dentry and mnt on success.
  726. * Returns error and drops reference to input namei data on failure.
  727. */
  728. static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
  729. {
  730. struct path next;
  731. struct inode *inode;
  732. int err;
  733. unsigned int lookup_flags = nd->flags;
  734. while (*name=='/')
  735. name++;
  736. if (!*name)
  737. goto return_reval;
  738. inode = nd->dentry->d_inode;
  739. if (nd->depth)
  740. lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
  741. /* At this point we know we have a real path component. */
  742. for(;;) {
  743. unsigned long hash;
  744. struct qstr this;
  745. unsigned int c;
  746. nd->flags |= LOOKUP_CONTINUE;
  747. err = exec_permission_lite(inode, nd);
  748. if (err == -EAGAIN)
  749. err = vfs_permission(nd, MAY_EXEC);
  750. if (err)
  751. break;
  752. this.name = name;
  753. c = *(const unsigned char *)name;
  754. hash = init_name_hash();
  755. do {
  756. name++;
  757. hash = partial_name_hash(c, hash);
  758. c = *(const unsigned char *)name;
  759. } while (c && (c != '/'));
  760. this.len = name - (const char *) this.name;
  761. this.hash = end_name_hash(hash);
  762. /* remove trailing slashes? */
  763. if (!c)
  764. goto last_component;
  765. while (*++name == '/');
  766. if (!*name)
  767. goto last_with_slashes;
  768. /*
  769. * "." and ".." are special - ".." especially so because it has
  770. * to be able to know about the current root directory and
  771. * parent relationships.
  772. */
  773. if (this.name[0] == '.') switch (this.len) {
  774. default:
  775. break;
  776. case 2:
  777. if (this.name[1] != '.')
  778. break;
  779. follow_dotdot(nd);
  780. inode = nd->dentry->d_inode;
  781. /* fallthrough */
  782. case 1:
  783. continue;
  784. }
  785. /*
  786. * See if the low-level filesystem might want
  787. * to use its own hash..
  788. */
  789. if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
  790. err = nd->dentry->d_op->d_hash(nd->dentry, &this);
  791. if (err < 0)
  792. break;
  793. }
  794. /* This does the actual lookups.. */
  795. err = do_lookup(nd, &this, &next);
  796. if (err)
  797. break;
  798. err = -ENOENT;
  799. inode = next.dentry->d_inode;
  800. if (!inode)
  801. goto out_dput;
  802. err = -ENOTDIR;
  803. if (!inode->i_op)
  804. goto out_dput;
  805. if (inode->i_op->follow_link) {
  806. err = do_follow_link(&next, nd);
  807. if (err)
  808. goto return_err;
  809. err = -ENOENT;
  810. inode = nd->dentry->d_inode;
  811. if (!inode)
  812. break;
  813. err = -ENOTDIR;
  814. if (!inode->i_op)
  815. break;
  816. } else
  817. path_to_nameidata(&next, nd);
  818. err = -ENOTDIR;
  819. if (!inode->i_op->lookup)
  820. break;
  821. continue;
  822. /* here ends the main loop */
  823. last_with_slashes:
  824. lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  825. last_component:
  826. /* Clear LOOKUP_CONTINUE iff it was previously unset */
  827. nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
  828. if (lookup_flags & LOOKUP_PARENT)
  829. goto lookup_parent;
  830. if (this.name[0] == '.') switch (this.len) {
  831. default:
  832. break;
  833. case 2:
  834. if (this.name[1] != '.')
  835. break;
  836. follow_dotdot(nd);
  837. inode = nd->dentry->d_inode;
  838. /* fallthrough */
  839. case 1:
  840. goto return_reval;
  841. }
  842. if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
  843. err = nd->dentry->d_op->d_hash(nd->dentry, &this);
  844. if (err < 0)
  845. break;
  846. }
  847. err = do_lookup(nd, &this, &next);
  848. if (err)
  849. break;
  850. inode = next.dentry->d_inode;
  851. if ((lookup_flags & LOOKUP_FOLLOW)
  852. && inode && inode->i_op && inode->i_op->follow_link) {
  853. err = do_follow_link(&next, nd);
  854. if (err)
  855. goto return_err;
  856. inode = nd->dentry->d_inode;
  857. } else
  858. path_to_nameidata(&next, nd);
  859. err = -ENOENT;
  860. if (!inode)
  861. break;
  862. if (lookup_flags & LOOKUP_DIRECTORY) {
  863. err = -ENOTDIR;
  864. if (!inode->i_op || !inode->i_op->lookup)
  865. break;
  866. }
  867. goto return_base;
  868. lookup_parent:
  869. nd->last = this;
  870. nd->last_type = LAST_NORM;
  871. if (this.name[0] != '.')
  872. goto return_base;
  873. if (this.len == 1)
  874. nd->last_type = LAST_DOT;
  875. else if (this.len == 2 && this.name[1] == '.')
  876. nd->last_type = LAST_DOTDOT;
  877. else
  878. goto return_base;
  879. return_reval:
  880. /*
  881. * We bypassed the ordinary revalidation routines.
  882. * We may need to check the cached dentry for staleness.
  883. */
  884. if (nd->dentry && nd->dentry->d_sb &&
  885. (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
  886. err = -ESTALE;
  887. /* Note: we do not d_invalidate() */
  888. if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
  889. break;
  890. }
  891. return_base:
  892. return 0;
  893. out_dput:
  894. dput_path(&next, nd);
  895. break;
  896. }
  897. path_release(nd);
  898. return_err:
  899. return err;
  900. }
  901. /*
  902. * Wrapper to retry pathname resolution whenever the underlying
  903. * file system returns an ESTALE.
  904. *
  905. * Retry the whole path once, forcing real lookup requests
  906. * instead of relying on the dcache.
  907. */
  908. int fastcall link_path_walk(const char *name, struct nameidata *nd)
  909. {
  910. struct nameidata save = *nd;
  911. int result;
  912. /* make sure the stuff we saved doesn't go away */
  913. dget(save.dentry);
  914. mntget(save.mnt);
  915. result = __link_path_walk(name, nd);
  916. if (result == -ESTALE) {
  917. *nd = save;
  918. dget(nd->dentry);
  919. mntget(nd->mnt);
  920. nd->flags |= LOOKUP_REVAL;
  921. result = __link_path_walk(name, nd);
  922. }
  923. dput(save.dentry);
  924. mntput(save.mnt);
  925. return result;
  926. }
  927. int fastcall path_walk(const char * name, struct nameidata *nd)
  928. {
  929. current->total_link_count = 0;
  930. return link_path_walk(name, nd);
  931. }
  932. /*
  933. * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
  934. * everything is done. Returns 0 and drops input nd, if lookup failed;
  935. */
  936. static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
  937. {
  938. if (path_walk(name, nd))
  939. return 0; /* something went wrong... */
  940. if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
  941. struct dentry *old_dentry = nd->dentry;
  942. struct vfsmount *old_mnt = nd->mnt;
  943. struct qstr last = nd->last;
  944. int last_type = nd->last_type;
  945. struct fs_struct *fs = current->fs;
  946. /*
  947. * NAME was not found in alternate root or it's a directory.
  948. * Try to find it in the normal root:
  949. */
  950. nd->last_type = LAST_ROOT;
  951. read_lock(&fs->lock);
  952. nd->mnt = mntget(fs->rootmnt);
  953. nd->dentry = dget(fs->root);
  954. read_unlock(&fs->lock);
  955. if (path_walk(name, nd) == 0) {
  956. if (nd->dentry->d_inode) {
  957. dput(old_dentry);
  958. mntput(old_mnt);
  959. return 1;
  960. }
  961. path_release(nd);
  962. }
  963. nd->dentry = old_dentry;
  964. nd->mnt = old_mnt;
  965. nd->last = last;
  966. nd->last_type = last_type;
  967. }
  968. return 1;
  969. }
  970. void set_fs_altroot(void)
  971. {
  972. char *emul = __emul_prefix();
  973. struct nameidata nd;
  974. struct vfsmount *mnt = NULL, *oldmnt;
  975. struct dentry *dentry = NULL, *olddentry;
  976. int err;
  977. struct fs_struct *fs = current->fs;
  978. if (!emul)
  979. goto set_it;
  980. err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
  981. if (!err) {
  982. mnt = nd.mnt;
  983. dentry = nd.dentry;
  984. }
  985. set_it:
  986. write_lock(&fs->lock);
  987. oldmnt = fs->altrootmnt;
  988. olddentry = fs->altroot;
  989. fs->altrootmnt = mnt;
  990. fs->altroot = dentry;
  991. write_unlock(&fs->lock);
  992. if (olddentry) {
  993. dput(olddentry);
  994. mntput(oldmnt);
  995. }
  996. }
  997. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  998. static int fastcall do_path_lookup(int dfd, const char *name,
  999. unsigned int flags, struct nameidata *nd)
  1000. {
  1001. int retval = 0;
  1002. int fput_needed;
  1003. struct file *file;
  1004. struct fs_struct *fs = current->fs;
  1005. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1006. nd->flags = flags;
  1007. nd->depth = 0;
  1008. if (*name=='/') {
  1009. read_lock(&fs->lock);
  1010. if (fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
  1011. nd->mnt = mntget(fs->altrootmnt);
  1012. nd->dentry = dget(fs->altroot);
  1013. read_unlock(&fs->lock);
  1014. if (__emul_lookup_dentry(name,nd))
  1015. goto out; /* found in altroot */
  1016. read_lock(&fs->lock);
  1017. }
  1018. nd->mnt = mntget(fs->rootmnt);
  1019. nd->dentry = dget(fs->root);
  1020. read_unlock(&fs->lock);
  1021. } else if (dfd == AT_FDCWD) {
  1022. read_lock(&fs->lock);
  1023. nd->mnt = mntget(fs->pwdmnt);
  1024. nd->dentry = dget(fs->pwd);
  1025. read_unlock(&fs->lock);
  1026. } else {
  1027. struct dentry *dentry;
  1028. file = fget_light(dfd, &fput_needed);
  1029. retval = -EBADF;
  1030. if (!file)
  1031. goto out_fail;
  1032. dentry = file->f_dentry;
  1033. retval = -ENOTDIR;
  1034. if (!S_ISDIR(dentry->d_inode->i_mode))
  1035. goto fput_fail;
  1036. retval = file_permission(file, MAY_EXEC);
  1037. if (retval)
  1038. goto fput_fail;
  1039. nd->mnt = mntget(file->f_vfsmnt);
  1040. nd->dentry = dget(dentry);
  1041. fput_light(file, fput_needed);
  1042. }
  1043. current->total_link_count = 0;
  1044. retval = link_path_walk(name, nd);
  1045. out:
  1046. if (likely(retval == 0)) {
  1047. if (unlikely(!audit_dummy_context() && nd && nd->dentry &&
  1048. nd->dentry->d_inode))
  1049. audit_inode(name, nd->dentry->d_inode);
  1050. }
  1051. out_fail:
  1052. return retval;
  1053. fput_fail:
  1054. fput_light(file, fput_needed);
  1055. goto out_fail;
  1056. }
  1057. int fastcall path_lookup(const char *name, unsigned int flags,
  1058. struct nameidata *nd)
  1059. {
  1060. return do_path_lookup(AT_FDCWD, name, flags, nd);
  1061. }
  1062. static int __path_lookup_intent_open(int dfd, const char *name,
  1063. unsigned int lookup_flags, struct nameidata *nd,
  1064. int open_flags, int create_mode)
  1065. {
  1066. struct file *filp = get_empty_filp();
  1067. int err;
  1068. if (filp == NULL)
  1069. return -ENFILE;
  1070. nd->intent.open.file = filp;
  1071. nd->intent.open.flags = open_flags;
  1072. nd->intent.open.create_mode = create_mode;
  1073. err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
  1074. if (IS_ERR(nd->intent.open.file)) {
  1075. if (err == 0) {
  1076. err = PTR_ERR(nd->intent.open.file);
  1077. path_release(nd);
  1078. }
  1079. } else if (err != 0)
  1080. release_open_intent(nd);
  1081. return err;
  1082. }
  1083. /**
  1084. * path_lookup_open - lookup a file path with open intent
  1085. * @dfd: the directory to use as base, or AT_FDCWD
  1086. * @name: pointer to file name
  1087. * @lookup_flags: lookup intent flags
  1088. * @nd: pointer to nameidata
  1089. * @open_flags: open intent flags
  1090. */
  1091. int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
  1092. struct nameidata *nd, int open_flags)
  1093. {
  1094. return __path_lookup_intent_open(dfd, name, lookup_flags, nd,
  1095. open_flags, 0);
  1096. }
  1097. /**
  1098. * path_lookup_create - lookup a file path with open + create intent
  1099. * @dfd: the directory to use as base, or AT_FDCWD
  1100. * @name: pointer to file name
  1101. * @lookup_flags: lookup intent flags
  1102. * @nd: pointer to nameidata
  1103. * @open_flags: open intent flags
  1104. * @create_mode: create intent flags
  1105. */
  1106. static int path_lookup_create(int dfd, const char *name,
  1107. unsigned int lookup_flags, struct nameidata *nd,
  1108. int open_flags, int create_mode)
  1109. {
  1110. return __path_lookup_intent_open(dfd, name, lookup_flags|LOOKUP_CREATE,
  1111. nd, open_flags, create_mode);
  1112. }
  1113. int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
  1114. struct nameidata *nd, int open_flags)
  1115. {
  1116. char *tmp = getname(name);
  1117. int err = PTR_ERR(tmp);
  1118. if (!IS_ERR(tmp)) {
  1119. err = __path_lookup_intent_open(AT_FDCWD, tmp, lookup_flags, nd, open_flags, 0);
  1120. putname(tmp);
  1121. }
  1122. return err;
  1123. }
  1124. /*
  1125. * Restricted form of lookup. Doesn't follow links, single-component only,
  1126. * needs parent already locked. Doesn't follow mounts.
  1127. * SMP-safe.
  1128. */
  1129. static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
  1130. {
  1131. struct dentry * dentry;
  1132. struct inode *inode;
  1133. int err;
  1134. inode = base->d_inode;
  1135. err = permission(inode, MAY_EXEC, nd);
  1136. dentry = ERR_PTR(err);
  1137. if (err)
  1138. goto out;
  1139. /*
  1140. * See if the low-level filesystem might want
  1141. * to use its own hash..
  1142. */
  1143. if (base->d_op && base->d_op->d_hash) {
  1144. err = base->d_op->d_hash(base, name);
  1145. dentry = ERR_PTR(err);
  1146. if (err < 0)
  1147. goto out;
  1148. }
  1149. dentry = cached_lookup(base, name, nd);
  1150. if (!dentry) {
  1151. struct dentry *new = d_alloc(base, name);
  1152. dentry = ERR_PTR(-ENOMEM);
  1153. if (!new)
  1154. goto out;
  1155. dentry = inode->i_op->lookup(inode, new, nd);
  1156. if (!dentry)
  1157. dentry = new;
  1158. else
  1159. dput(new);
  1160. }
  1161. out:
  1162. return dentry;
  1163. }
  1164. static struct dentry *lookup_hash(struct nameidata *nd)
  1165. {
  1166. return __lookup_hash(&nd->last, nd->dentry, nd);
  1167. }
  1168. /* SMP-safe */
  1169. struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
  1170. {
  1171. unsigned long hash;
  1172. struct qstr this;
  1173. unsigned int c;
  1174. this.name = name;
  1175. this.len = len;
  1176. if (!len)
  1177. goto access;
  1178. hash = init_name_hash();
  1179. while (len--) {
  1180. c = *(const unsigned char *)name++;
  1181. if (c == '/' || c == '\0')
  1182. goto access;
  1183. hash = partial_name_hash(c, hash);
  1184. }
  1185. this.hash = end_name_hash(hash);
  1186. return __lookup_hash(&this, base, NULL);
  1187. access:
  1188. return ERR_PTR(-EACCES);
  1189. }
  1190. /*
  1191. * namei()
  1192. *
  1193. * is used by most simple commands to get the inode of a specified name.
  1194. * Open, link etc use their own routines, but this is enough for things
  1195. * like 'chmod' etc.
  1196. *
  1197. * namei exists in two versions: namei/lnamei. The only difference is
  1198. * that namei follows links, while lnamei does not.
  1199. * SMP-safe
  1200. */
  1201. int fastcall __user_walk_fd(int dfd, const char __user *name, unsigned flags,
  1202. struct nameidata *nd)
  1203. {
  1204. char *tmp = getname(name);
  1205. int err = PTR_ERR(tmp);
  1206. if (!IS_ERR(tmp)) {
  1207. err = do_path_lookup(dfd, tmp, flags, nd);
  1208. putname(tmp);
  1209. }
  1210. return err;
  1211. }
  1212. int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
  1213. {
  1214. return __user_walk_fd(AT_FDCWD, name, flags, nd);
  1215. }
  1216. /*
  1217. * It's inline, so penalty for filesystems that don't use sticky bit is
  1218. * minimal.
  1219. */
  1220. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1221. {
  1222. if (!(dir->i_mode & S_ISVTX))
  1223. return 0;
  1224. if (inode->i_uid == current->fsuid)
  1225. return 0;
  1226. if (dir->i_uid == current->fsuid)
  1227. return 0;
  1228. return !capable(CAP_FOWNER);
  1229. }
  1230. /*
  1231. * Check whether we can remove a link victim from directory dir, check
  1232. * whether the type of victim is right.
  1233. * 1. We can't do it if dir is read-only (done in permission())
  1234. * 2. We should have write and exec permissions on dir
  1235. * 3. We can't remove anything from append-only dir
  1236. * 4. We can't do anything with immutable dir (done in permission())
  1237. * 5. If the sticky bit on dir is set we should either
  1238. * a. be owner of dir, or
  1239. * b. be owner of victim, or
  1240. * c. have CAP_FOWNER capability
  1241. * 6. If the victim is append-only or immutable we can't do antyhing with
  1242. * links pointing to it.
  1243. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1244. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1245. * 9. We can't remove a root or mountpoint.
  1246. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1247. * nfs_async_unlink().
  1248. */
  1249. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1250. {
  1251. int error;
  1252. if (!victim->d_inode)
  1253. return -ENOENT;
  1254. BUG_ON(victim->d_parent->d_inode != dir);
  1255. audit_inode_child(victim->d_name.name, victim->d_inode, dir);
  1256. error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
  1257. if (error)
  1258. return error;
  1259. if (IS_APPEND(dir))
  1260. return -EPERM;
  1261. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1262. IS_IMMUTABLE(victim->d_inode))
  1263. return -EPERM;
  1264. if (isdir) {
  1265. if (!S_ISDIR(victim->d_inode->i_mode))
  1266. return -ENOTDIR;
  1267. if (IS_ROOT(victim))
  1268. return -EBUSY;
  1269. } else if (S_ISDIR(victim->d_inode->i_mode))
  1270. return -EISDIR;
  1271. if (IS_DEADDIR(dir))
  1272. return -ENOENT;
  1273. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1274. return -EBUSY;
  1275. return 0;
  1276. }
  1277. /* Check whether we can create an object with dentry child in directory
  1278. * dir.
  1279. * 1. We can't do it if child already exists (open has special treatment for
  1280. * this case, but since we are inlined it's OK)
  1281. * 2. We can't do it if dir is read-only (done in permission())
  1282. * 3. We should have write and exec permissions on dir
  1283. * 4. We can't do it if dir is immutable (done in permission())
  1284. */
  1285. static inline int may_create(struct inode *dir, struct dentry *child,
  1286. struct nameidata *nd)
  1287. {
  1288. if (child->d_inode)
  1289. return -EEXIST;
  1290. if (IS_DEADDIR(dir))
  1291. return -ENOENT;
  1292. return permission(dir,MAY_WRITE | MAY_EXEC, nd);
  1293. }
  1294. /*
  1295. * O_DIRECTORY translates into forcing a directory lookup.
  1296. */
  1297. static inline int lookup_flags(unsigned int f)
  1298. {
  1299. unsigned long retval = LOOKUP_FOLLOW;
  1300. if (f & O_NOFOLLOW)
  1301. retval &= ~LOOKUP_FOLLOW;
  1302. if (f & O_DIRECTORY)
  1303. retval |= LOOKUP_DIRECTORY;
  1304. return retval;
  1305. }
  1306. /*
  1307. * p1 and p2 should be directories on the same fs.
  1308. */
  1309. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1310. {
  1311. struct dentry *p;
  1312. if (p1 == p2) {
  1313. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1314. return NULL;
  1315. }
  1316. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1317. for (p = p1; p->d_parent != p; p = p->d_parent) {
  1318. if (p->d_parent == p2) {
  1319. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1320. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1321. return p;
  1322. }
  1323. }
  1324. for (p = p2; p->d_parent != p; p = p->d_parent) {
  1325. if (p->d_parent == p1) {
  1326. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1327. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1328. return p;
  1329. }
  1330. }
  1331. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1332. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1333. return NULL;
  1334. }
  1335. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1336. {
  1337. mutex_unlock(&p1->d_inode->i_mutex);
  1338. if (p1 != p2) {
  1339. mutex_unlock(&p2->d_inode->i_mutex);
  1340. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1341. }
  1342. }
  1343. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1344. struct nameidata *nd)
  1345. {
  1346. int error = may_create(dir, dentry, nd);
  1347. if (error)
  1348. return error;
  1349. if (!dir->i_op || !dir->i_op->create)
  1350. return -EACCES; /* shouldn't it be ENOSYS? */
  1351. mode &= S_IALLUGO;
  1352. mode |= S_IFREG;
  1353. error = security_inode_create(dir, dentry, mode);
  1354. if (error)
  1355. return error;
  1356. DQUOT_INIT(dir);
  1357. error = dir->i_op->create(dir, dentry, mode, nd);
  1358. if (!error)
  1359. fsnotify_create(dir, dentry);
  1360. return error;
  1361. }
  1362. int may_open(struct nameidata *nd, int acc_mode, int flag)
  1363. {
  1364. struct dentry *dentry = nd->dentry;
  1365. struct inode *inode = dentry->d_inode;
  1366. int error;
  1367. if (!inode)
  1368. return -ENOENT;
  1369. if (S_ISLNK(inode->i_mode))
  1370. return -ELOOP;
  1371. if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
  1372. return -EISDIR;
  1373. error = vfs_permission(nd, acc_mode);
  1374. if (error)
  1375. return error;
  1376. /*
  1377. * FIFO's, sockets and device files are special: they don't
  1378. * actually live on the filesystem itself, and as such you
  1379. * can write to them even if the filesystem is read-only.
  1380. */
  1381. if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  1382. flag &= ~O_TRUNC;
  1383. } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
  1384. if (nd->mnt->mnt_flags & MNT_NODEV)
  1385. return -EACCES;
  1386. flag &= ~O_TRUNC;
  1387. } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
  1388. return -EROFS;
  1389. /*
  1390. * An append-only file must be opened in append mode for writing.
  1391. */
  1392. if (IS_APPEND(inode)) {
  1393. if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
  1394. return -EPERM;
  1395. if (flag & O_TRUNC)
  1396. return -EPERM;
  1397. }
  1398. /* O_NOATIME can only be set by the owner or superuser */
  1399. if (flag & O_NOATIME)
  1400. if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
  1401. return -EPERM;
  1402. /*
  1403. * Ensure there are no outstanding leases on the file.
  1404. */
  1405. error = break_lease(inode, flag);
  1406. if (error)
  1407. return error;
  1408. if (flag & O_TRUNC) {
  1409. error = get_write_access(inode);
  1410. if (error)
  1411. return error;
  1412. /*
  1413. * Refuse to truncate files with mandatory locks held on them.
  1414. */
  1415. error = locks_verify_locked(inode);
  1416. if (!error) {
  1417. DQUOT_INIT(inode);
  1418. error = do_truncate(dentry, 0, ATTR_MTIME|ATTR_CTIME, NULL);
  1419. }
  1420. put_write_access(inode);
  1421. if (error)
  1422. return error;
  1423. } else
  1424. if (flag & FMODE_WRITE)
  1425. DQUOT_INIT(inode);
  1426. return 0;
  1427. }
  1428. /*
  1429. * open_namei()
  1430. *
  1431. * namei for open - this is in fact almost the whole open-routine.
  1432. *
  1433. * Note that the low bits of "flag" aren't the same as in the open
  1434. * system call - they are 00 - no permissions needed
  1435. * 01 - read permission needed
  1436. * 10 - write permission needed
  1437. * 11 - read/write permissions needed
  1438. * which is a lot more logical, and also allows the "no perm" needed
  1439. * for symlinks (where the permissions are checked later).
  1440. * SMP-safe
  1441. */
  1442. int open_namei(int dfd, const char *pathname, int flag,
  1443. int mode, struct nameidata *nd)
  1444. {
  1445. int acc_mode, error;
  1446. struct path path;
  1447. struct dentry *dir;
  1448. int count = 0;
  1449. acc_mode = ACC_MODE(flag);
  1450. /* O_TRUNC implies we need access checks for write permissions */
  1451. if (flag & O_TRUNC)
  1452. acc_mode |= MAY_WRITE;
  1453. /* Allow the LSM permission hook to distinguish append
  1454. access from general write access. */
  1455. if (flag & O_APPEND)
  1456. acc_mode |= MAY_APPEND;
  1457. /*
  1458. * The simplest case - just a plain lookup.
  1459. */
  1460. if (!(flag & O_CREAT)) {
  1461. error = path_lookup_open(dfd, pathname, lookup_flags(flag),
  1462. nd, flag);
  1463. if (error)
  1464. return error;
  1465. goto ok;
  1466. }
  1467. /*
  1468. * Create - we need to know the parent.
  1469. */
  1470. error = path_lookup_create(dfd,pathname,LOOKUP_PARENT,nd,flag,mode);
  1471. if (error)
  1472. return error;
  1473. /*
  1474. * We have the parent and last component. First of all, check
  1475. * that we are not asked to creat(2) an obvious directory - that
  1476. * will not do.
  1477. */
  1478. error = -EISDIR;
  1479. if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
  1480. goto exit;
  1481. dir = nd->dentry;
  1482. nd->flags &= ~LOOKUP_PARENT;
  1483. mutex_lock(&dir->d_inode->i_mutex);
  1484. path.dentry = lookup_hash(nd);
  1485. path.mnt = nd->mnt;
  1486. do_last:
  1487. error = PTR_ERR(path.dentry);
  1488. if (IS_ERR(path.dentry)) {
  1489. mutex_unlock(&dir->d_inode->i_mutex);
  1490. goto exit;
  1491. }
  1492. if (IS_ERR(nd->intent.open.file)) {
  1493. mutex_unlock(&dir->d_inode->i_mutex);
  1494. error = PTR_ERR(nd->intent.open.file);
  1495. goto exit_dput;
  1496. }
  1497. /* Negative dentry, just create the file */
  1498. if (!path.dentry->d_inode) {
  1499. if (!IS_POSIXACL(dir->d_inode))
  1500. mode &= ~current->fs->umask;
  1501. error = vfs_create(dir->d_inode, path.dentry, mode, nd);
  1502. mutex_unlock(&dir->d_inode->i_mutex);
  1503. dput(nd->dentry);
  1504. nd->dentry = path.dentry;
  1505. if (error)
  1506. goto exit;
  1507. /* Don't check for write permission, don't truncate */
  1508. acc_mode = 0;
  1509. flag &= ~O_TRUNC;
  1510. goto ok;
  1511. }
  1512. /*
  1513. * It already exists.
  1514. */
  1515. mutex_unlock(&dir->d_inode->i_mutex);
  1516. audit_inode_update(path.dentry->d_inode);
  1517. error = -EEXIST;
  1518. if (flag & O_EXCL)
  1519. goto exit_dput;
  1520. if (__follow_mount(&path)) {
  1521. error = -ELOOP;
  1522. if (flag & O_NOFOLLOW)
  1523. goto exit_dput;
  1524. }
  1525. error = -ENOENT;
  1526. if (!path.dentry->d_inode)
  1527. goto exit_dput;
  1528. if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
  1529. goto do_link;
  1530. path_to_nameidata(&path, nd);
  1531. error = -EISDIR;
  1532. if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
  1533. goto exit;
  1534. ok:
  1535. error = may_open(nd, acc_mode, flag);
  1536. if (error)
  1537. goto exit;
  1538. return 0;
  1539. exit_dput:
  1540. dput_path(&path, nd);
  1541. exit:
  1542. if (!IS_ERR(nd->intent.open.file))
  1543. release_open_intent(nd);
  1544. path_release(nd);
  1545. return error;
  1546. do_link:
  1547. error = -ELOOP;
  1548. if (flag & O_NOFOLLOW)
  1549. goto exit_dput;
  1550. /*
  1551. * This is subtle. Instead of calling do_follow_link() we do the
  1552. * thing by hands. The reason is that this way we have zero link_count
  1553. * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
  1554. * After that we have the parent and last component, i.e.
  1555. * we are in the same situation as after the first path_walk().
  1556. * Well, almost - if the last component is normal we get its copy
  1557. * stored in nd->last.name and we will have to putname() it when we
  1558. * are done. Procfs-like symlinks just set LAST_BIND.
  1559. */
  1560. nd->flags |= LOOKUP_PARENT;
  1561. error = security_inode_follow_link(path.dentry, nd);
  1562. if (error)
  1563. goto exit_dput;
  1564. error = __do_follow_link(&path, nd);
  1565. if (error) {
  1566. /* Does someone understand code flow here? Or it is only
  1567. * me so stupid? Anathema to whoever designed this non-sense
  1568. * with "intent.open".
  1569. */
  1570. release_open_intent(nd);
  1571. return error;
  1572. }
  1573. nd->flags &= ~LOOKUP_PARENT;
  1574. if (nd->last_type == LAST_BIND)
  1575. goto ok;
  1576. error = -EISDIR;
  1577. if (nd->last_type != LAST_NORM)
  1578. goto exit;
  1579. if (nd->last.name[nd->last.len]) {
  1580. __putname(nd->last.name);
  1581. goto exit;
  1582. }
  1583. error = -ELOOP;
  1584. if (count++==32) {
  1585. __putname(nd->last.name);
  1586. goto exit;
  1587. }
  1588. dir = nd->dentry;
  1589. mutex_lock(&dir->d_inode->i_mutex);
  1590. path.dentry = lookup_hash(nd);
  1591. path.mnt = nd->mnt;
  1592. __putname(nd->last.name);
  1593. goto do_last;
  1594. }
  1595. /**
  1596. * lookup_create - lookup a dentry, creating it if it doesn't exist
  1597. * @nd: nameidata info
  1598. * @is_dir: directory flag
  1599. *
  1600. * Simple function to lookup and return a dentry and create it
  1601. * if it doesn't exist. Is SMP-safe.
  1602. *
  1603. * Returns with nd->dentry->d_inode->i_mutex locked.
  1604. */
  1605. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  1606. {
  1607. struct dentry *dentry = ERR_PTR(-EEXIST);
  1608. mutex_lock_nested(&nd->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1609. /*
  1610. * Yucky last component or no last component at all?
  1611. * (foo/., foo/.., /////)
  1612. */
  1613. if (nd->last_type != LAST_NORM)
  1614. goto fail;
  1615. nd->flags &= ~LOOKUP_PARENT;
  1616. nd->flags |= LOOKUP_CREATE;
  1617. nd->intent.open.flags = O_EXCL;
  1618. /*
  1619. * Do the final lookup.
  1620. */
  1621. dentry = lookup_hash(nd);
  1622. if (IS_ERR(dentry))
  1623. goto fail;
  1624. /*
  1625. * Special case - lookup gave negative, but... we had foo/bar/
  1626. * From the vfs_mknod() POV we just have a negative dentry -
  1627. * all is fine. Let's be bastards - you had / on the end, you've
  1628. * been asking for (non-existent) directory. -ENOENT for you.
  1629. */
  1630. if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
  1631. goto enoent;
  1632. return dentry;
  1633. enoent:
  1634. dput(dentry);
  1635. dentry = ERR_PTR(-ENOENT);
  1636. fail:
  1637. return dentry;
  1638. }
  1639. EXPORT_SYMBOL_GPL(lookup_create);
  1640. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1641. {
  1642. int error = may_create(dir, dentry, NULL);
  1643. if (error)
  1644. return error;
  1645. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  1646. return -EPERM;
  1647. if (!dir->i_op || !dir->i_op->mknod)
  1648. return -EPERM;
  1649. error = security_inode_mknod(dir, dentry, mode, dev);
  1650. if (error)
  1651. return error;
  1652. DQUOT_INIT(dir);
  1653. error = dir->i_op->mknod(dir, dentry, mode, dev);
  1654. if (!error)
  1655. fsnotify_create(dir, dentry);
  1656. return error;
  1657. }
  1658. asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
  1659. unsigned dev)
  1660. {
  1661. int error = 0;
  1662. char * tmp;
  1663. struct dentry * dentry;
  1664. struct nameidata nd;
  1665. if (S_ISDIR(mode))
  1666. return -EPERM;
  1667. tmp = getname(filename);
  1668. if (IS_ERR(tmp))
  1669. return PTR_ERR(tmp);
  1670. error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
  1671. if (error)
  1672. goto out;
  1673. dentry = lookup_create(&nd, 0);
  1674. error = PTR_ERR(dentry);
  1675. if (!IS_POSIXACL(nd.dentry->d_inode))
  1676. mode &= ~current->fs->umask;
  1677. if (!IS_ERR(dentry)) {
  1678. switch (mode & S_IFMT) {
  1679. case 0: case S_IFREG:
  1680. error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
  1681. break;
  1682. case S_IFCHR: case S_IFBLK:
  1683. error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
  1684. new_decode_dev(dev));
  1685. break;
  1686. case S_IFIFO: case S_IFSOCK:
  1687. error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
  1688. break;
  1689. case S_IFDIR:
  1690. error = -EPERM;
  1691. break;
  1692. default:
  1693. error = -EINVAL;
  1694. }
  1695. dput(dentry);
  1696. }
  1697. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1698. path_release(&nd);
  1699. out:
  1700. putname(tmp);
  1701. return error;
  1702. }
  1703. asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
  1704. {
  1705. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  1706. }
  1707. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1708. {
  1709. int error = may_create(dir, dentry, NULL);
  1710. if (error)
  1711. return error;
  1712. if (!dir->i_op || !dir->i_op->mkdir)
  1713. return -EPERM;
  1714. mode &= (S_IRWXUGO|S_ISVTX);
  1715. error = security_inode_mkdir(dir, dentry, mode);
  1716. if (error)
  1717. return error;
  1718. DQUOT_INIT(dir);
  1719. error = dir->i_op->mkdir(dir, dentry, mode);
  1720. if (!error)
  1721. fsnotify_mkdir(dir, dentry);
  1722. return error;
  1723. }
  1724. asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
  1725. {
  1726. int error = 0;
  1727. char * tmp;
  1728. tmp = getname(pathname);
  1729. error = PTR_ERR(tmp);
  1730. if (!IS_ERR(tmp)) {
  1731. struct dentry *dentry;
  1732. struct nameidata nd;
  1733. error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
  1734. if (error)
  1735. goto out;
  1736. dentry = lookup_create(&nd, 1);
  1737. error = PTR_ERR(dentry);
  1738. if (!IS_ERR(dentry)) {
  1739. if (!IS_POSIXACL(nd.dentry->d_inode))
  1740. mode &= ~current->fs->umask;
  1741. error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
  1742. dput(dentry);
  1743. }
  1744. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1745. path_release(&nd);
  1746. out:
  1747. putname(tmp);
  1748. }
  1749. return error;
  1750. }
  1751. asmlinkage long sys_mkdir(const char __user *pathname, int mode)
  1752. {
  1753. return sys_mkdirat(AT_FDCWD, pathname, mode);
  1754. }
  1755. /*
  1756. * We try to drop the dentry early: we should have
  1757. * a usage count of 2 if we're the only user of this
  1758. * dentry, and if that is true (possibly after pruning
  1759. * the dcache), then we drop the dentry now.
  1760. *
  1761. * A low-level filesystem can, if it choses, legally
  1762. * do a
  1763. *
  1764. * if (!d_unhashed(dentry))
  1765. * return -EBUSY;
  1766. *
  1767. * if it cannot handle the case of removing a directory
  1768. * that is still in use by something else..
  1769. */
  1770. void dentry_unhash(struct dentry *dentry)
  1771. {
  1772. dget(dentry);
  1773. if (atomic_read(&dentry->d_count))
  1774. shrink_dcache_parent(dentry);
  1775. spin_lock(&dcache_lock);
  1776. spin_lock(&dentry->d_lock);
  1777. if (atomic_read(&dentry->d_count) == 2)
  1778. __d_drop(dentry);
  1779. spin_unlock(&dentry->d_lock);
  1780. spin_unlock(&dcache_lock);
  1781. }
  1782. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  1783. {
  1784. int error = may_delete(dir, dentry, 1);
  1785. if (error)
  1786. return error;
  1787. if (!dir->i_op || !dir->i_op->rmdir)
  1788. return -EPERM;
  1789. DQUOT_INIT(dir);
  1790. mutex_lock(&dentry->d_inode->i_mutex);
  1791. dentry_unhash(dentry);
  1792. if (d_mountpoint(dentry))
  1793. error = -EBUSY;
  1794. else {
  1795. error = security_inode_rmdir(dir, dentry);
  1796. if (!error) {
  1797. error = dir->i_op->rmdir(dir, dentry);
  1798. if (!error)
  1799. dentry->d_inode->i_flags |= S_DEAD;
  1800. }
  1801. }
  1802. mutex_unlock(&dentry->d_inode->i_mutex);
  1803. if (!error) {
  1804. d_delete(dentry);
  1805. }
  1806. dput(dentry);
  1807. return error;
  1808. }
  1809. static long do_rmdir(int dfd, const char __user *pathname)
  1810. {
  1811. int error = 0;
  1812. char * name;
  1813. struct dentry *dentry;
  1814. struct nameidata nd;
  1815. name = getname(pathname);
  1816. if(IS_ERR(name))
  1817. return PTR_ERR(name);
  1818. error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
  1819. if (error)
  1820. goto exit;
  1821. switch(nd.last_type) {
  1822. case LAST_DOTDOT:
  1823. error = -ENOTEMPTY;
  1824. goto exit1;
  1825. case LAST_DOT:
  1826. error = -EINVAL;
  1827. goto exit1;
  1828. case LAST_ROOT:
  1829. error = -EBUSY;
  1830. goto exit1;
  1831. }
  1832. mutex_lock_nested(&nd.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1833. dentry = lookup_hash(&nd);
  1834. error = PTR_ERR(dentry);
  1835. if (!IS_ERR(dentry)) {
  1836. error = vfs_rmdir(nd.dentry->d_inode, dentry);
  1837. dput(dentry);
  1838. }
  1839. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1840. exit1:
  1841. path_release(&nd);
  1842. exit:
  1843. putname(name);
  1844. return error;
  1845. }
  1846. asmlinkage long sys_rmdir(const char __user *pathname)
  1847. {
  1848. return do_rmdir(AT_FDCWD, pathname);
  1849. }
  1850. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  1851. {
  1852. int error = may_delete(dir, dentry, 0);
  1853. if (error)
  1854. return error;
  1855. if (!dir->i_op || !dir->i_op->unlink)
  1856. return -EPERM;
  1857. DQUOT_INIT(dir);
  1858. mutex_lock(&dentry->d_inode->i_mutex);
  1859. if (d_mountpoint(dentry))
  1860. error = -EBUSY;
  1861. else {
  1862. error = security_inode_unlink(dir, dentry);
  1863. if (!error)
  1864. error = dir->i_op->unlink(dir, dentry);
  1865. }
  1866. mutex_unlock(&dentry->d_inode->i_mutex);
  1867. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  1868. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  1869. d_delete(dentry);
  1870. }
  1871. return error;
  1872. }
  1873. /*
  1874. * Make sure that the actual truncation of the file will occur outside its
  1875. * directory's i_mutex. Truncate can take a long time if there is a lot of
  1876. * writeout happening, and we don't want to prevent access to the directory
  1877. * while waiting on the I/O.
  1878. */
  1879. static long do_unlinkat(int dfd, const char __user *pathname)
  1880. {
  1881. int error = 0;
  1882. char * name;
  1883. struct dentry *dentry;
  1884. struct nameidata nd;
  1885. struct inode *inode = NULL;
  1886. name = getname(pathname);
  1887. if(IS_ERR(name))
  1888. return PTR_ERR(name);
  1889. error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
  1890. if (error)
  1891. goto exit;
  1892. error = -EISDIR;
  1893. if (nd.last_type != LAST_NORM)
  1894. goto exit1;
  1895. mutex_lock_nested(&nd.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1896. dentry = lookup_hash(&nd);
  1897. error = PTR_ERR(dentry);
  1898. if (!IS_ERR(dentry)) {
  1899. /* Why not before? Because we want correct error value */
  1900. if (nd.last.name[nd.last.len])
  1901. goto slashes;
  1902. inode = dentry->d_inode;
  1903. if (inode)
  1904. atomic_inc(&inode->i_count);
  1905. error = vfs_unlink(nd.dentry->d_inode, dentry);
  1906. exit2:
  1907. dput(dentry);
  1908. }
  1909. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1910. if (inode)
  1911. iput(inode); /* truncate the inode here */
  1912. exit1:
  1913. path_release(&nd);
  1914. exit:
  1915. putname(name);
  1916. return error;
  1917. slashes:
  1918. error = !dentry->d_inode ? -ENOENT :
  1919. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  1920. goto exit2;
  1921. }
  1922. asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
  1923. {
  1924. if ((flag & ~AT_REMOVEDIR) != 0)
  1925. return -EINVAL;
  1926. if (flag & AT_REMOVEDIR)
  1927. return do_rmdir(dfd, pathname);
  1928. return do_unlinkat(dfd, pathname);
  1929. }
  1930. asmlinkage long sys_unlink(const char __user *pathname)
  1931. {
  1932. return do_unlinkat(AT_FDCWD, pathname);
  1933. }
  1934. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
  1935. {
  1936. int error = may_create(dir, dentry, NULL);
  1937. if (error)
  1938. return error;
  1939. if (!dir->i_op || !dir->i_op->symlink)
  1940. return -EPERM;
  1941. error = security_inode_symlink(dir, dentry, oldname);
  1942. if (error)
  1943. return error;
  1944. DQUOT_INIT(dir);
  1945. error = dir->i_op->symlink(dir, dentry, oldname);
  1946. if (!error)
  1947. fsnotify_create(dir, dentry);
  1948. return error;
  1949. }
  1950. asmlinkage long sys_symlinkat(const char __user *oldname,
  1951. int newdfd, const char __user *newname)
  1952. {
  1953. int error = 0;
  1954. char * from;
  1955. char * to;
  1956. from = getname(oldname);
  1957. if(IS_ERR(from))
  1958. return PTR_ERR(from);
  1959. to = getname(newname);
  1960. error = PTR_ERR(to);
  1961. if (!IS_ERR(to)) {
  1962. struct dentry *dentry;
  1963. struct nameidata nd;
  1964. error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
  1965. if (error)
  1966. goto out;
  1967. dentry = lookup_create(&nd, 0);
  1968. error = PTR_ERR(dentry);
  1969. if (!IS_ERR(dentry)) {
  1970. error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
  1971. dput(dentry);
  1972. }
  1973. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1974. path_release(&nd);
  1975. out:
  1976. putname(to);
  1977. }
  1978. putname(from);
  1979. return error;
  1980. }
  1981. asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
  1982. {
  1983. return sys_symlinkat(oldname, AT_FDCWD, newname);
  1984. }
  1985. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  1986. {
  1987. struct inode *inode = old_dentry->d_inode;
  1988. int error;
  1989. if (!inode)
  1990. return -ENOENT;
  1991. error = may_create(dir, new_dentry, NULL);
  1992. if (error)
  1993. return error;
  1994. if (dir->i_sb != inode->i_sb)
  1995. return -EXDEV;
  1996. /*
  1997. * A link to an append-only or immutable file cannot be created.
  1998. */
  1999. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2000. return -EPERM;
  2001. if (!dir->i_op || !dir->i_op->link)
  2002. return -EPERM;
  2003. if (S_ISDIR(old_dentry->d_inode->i_mode))
  2004. return -EPERM;
  2005. error = security_inode_link(old_dentry, dir, new_dentry);
  2006. if (error)
  2007. return error;
  2008. mutex_lock(&old_dentry->d_inode->i_mutex);
  2009. DQUOT_INIT(dir);
  2010. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2011. mutex_unlock(&old_dentry->d_inode->i_mutex);
  2012. if (!error)
  2013. fsnotify_create(dir, new_dentry);
  2014. return error;
  2015. }
  2016. /*
  2017. * Hardlinks are often used in delicate situations. We avoid
  2018. * security-related surprises by not following symlinks on the
  2019. * newname. --KAB
  2020. *
  2021. * We don't follow them on the oldname either to be compatible
  2022. * with linux 2.0, and to avoid hard-linking to directories
  2023. * and other special files. --ADM
  2024. */
  2025. asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
  2026. int newdfd, const char __user *newname,
  2027. int flags)
  2028. {
  2029. struct dentry *new_dentry;
  2030. struct nameidata nd, old_nd;
  2031. int error;
  2032. char * to;
  2033. if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
  2034. return -EINVAL;
  2035. to = getname(newname);
  2036. if (IS_ERR(to))
  2037. return PTR_ERR(to);
  2038. error = __user_walk_fd(olddfd, oldname,
  2039. flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
  2040. &old_nd);
  2041. if (error)
  2042. goto exit;
  2043. error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
  2044. if (error)
  2045. goto out;
  2046. error = -EXDEV;
  2047. if (old_nd.mnt != nd.mnt)
  2048. goto out_release;
  2049. new_dentry = lookup_create(&nd, 0);
  2050. error = PTR_ERR(new_dentry);
  2051. if (!IS_ERR(new_dentry)) {
  2052. error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
  2053. dput(new_dentry);
  2054. }
  2055. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  2056. out_release:
  2057. path_release(&nd);
  2058. out:
  2059. path_release(&old_nd);
  2060. exit:
  2061. putname(to);
  2062. return error;
  2063. }
  2064. asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
  2065. {
  2066. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2067. }
  2068. /*
  2069. * The worst of all namespace operations - renaming directory. "Perverted"
  2070. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2071. * Problems:
  2072. * a) we can get into loop creation. Check is done in is_subdir().
  2073. * b) race potential - two innocent renames can create a loop together.
  2074. * That's where 4.4 screws up. Current fix: serialization on
  2075. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2076. * story.
  2077. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2078. * And that - after we got ->i_mutex on parents (until then we don't know
  2079. * whether the target exists). Solution: try to be smart with locking
  2080. * order for inodes. We rely on the fact that tree topology may change
  2081. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2082. * move will be locked. Thus we can rank directories by the tree
  2083. * (ancestors first) and rank all non-directories after them.
  2084. * That works since everybody except rename does "lock parent, lookup,
  2085. * lock child" and rename is under ->s_vfs_rename_mutex.
  2086. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2087. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2088. * we'd better make sure that there's no link(2) for them.
  2089. * d) some filesystems don't support opened-but-unlinked directories,
  2090. * either because of layout or because they are not ready to deal with
  2091. * all cases correctly. The latter will be fixed (taking this sort of
  2092. * stuff into VFS), but the former is not going away. Solution: the same
  2093. * trick as in rmdir().
  2094. * e) conversion from fhandle to dentry may come in the wrong moment - when
  2095. * we are removing the target. Solution: we will have to grab ->i_mutex
  2096. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2097. * ->i_mutex on parents, which works but leads to some truely excessive
  2098. * locking].
  2099. */
  2100. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2101. struct inode *new_dir, struct dentry *new_dentry)
  2102. {
  2103. int error = 0;
  2104. struct inode *target;
  2105. /*
  2106. * If we are going to change the parent - check write permissions,
  2107. * we'll need to flip '..'.
  2108. */
  2109. if (new_dir != old_dir) {
  2110. error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
  2111. if (error)
  2112. return error;
  2113. }
  2114. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2115. if (error)
  2116. return error;
  2117. target = new_dentry->d_inode;
  2118. if (target) {
  2119. mutex_lock(&target->i_mutex);
  2120. dentry_unhash(new_dentry);
  2121. }
  2122. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2123. error = -EBUSY;
  2124. else
  2125. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2126. if (target) {
  2127. if (!error)
  2128. target->i_flags |= S_DEAD;
  2129. mutex_unlock(&target->i_mutex);
  2130. if (d_unhashed(new_dentry))
  2131. d_rehash(new_dentry);
  2132. dput(new_dentry);
  2133. }
  2134. if (!error)
  2135. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2136. d_move(old_dentry,new_dentry);
  2137. return error;
  2138. }
  2139. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2140. struct inode *new_dir, struct dentry *new_dentry)
  2141. {
  2142. struct inode *target;
  2143. int error;
  2144. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2145. if (error)
  2146. return error;
  2147. dget(new_dentry);
  2148. target = new_dentry->d_inode;
  2149. if (target)
  2150. mutex_lock(&target->i_mutex);
  2151. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2152. error = -EBUSY;
  2153. else
  2154. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2155. if (!error) {
  2156. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2157. d_move(old_dentry, new_dentry);
  2158. }
  2159. if (target)
  2160. mutex_unlock(&target->i_mutex);
  2161. dput(new_dentry);
  2162. return error;
  2163. }
  2164. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2165. struct inode *new_dir, struct dentry *new_dentry)
  2166. {
  2167. int error;
  2168. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2169. const char *old_name;
  2170. if (old_dentry->d_inode == new_dentry->d_inode)
  2171. return 0;
  2172. error = may_delete(old_dir, old_dentry, is_dir);
  2173. if (error)
  2174. return error;
  2175. if (!new_dentry->d_inode)
  2176. error = may_create(new_dir, new_dentry, NULL);
  2177. else
  2178. error = may_delete(new_dir, new_dentry, is_dir);
  2179. if (error)
  2180. return error;
  2181. if (!old_dir->i_op || !old_dir->i_op->rename)
  2182. return -EPERM;
  2183. DQUOT_INIT(old_dir);
  2184. DQUOT_INIT(new_dir);
  2185. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2186. if (is_dir)
  2187. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2188. else
  2189. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2190. if (!error) {
  2191. const char *new_name = old_dentry->d_name.name;
  2192. fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
  2193. new_dentry->d_inode, old_dentry->d_inode);
  2194. }
  2195. fsnotify_oldname_free(old_name);
  2196. return error;
  2197. }
  2198. static int do_rename(int olddfd, const char *oldname,
  2199. int newdfd, const char *newname)
  2200. {
  2201. int error = 0;
  2202. struct dentry * old_dir, * new_dir;
  2203. struct dentry * old_dentry, *new_dentry;
  2204. struct dentry * trap;
  2205. struct nameidata oldnd, newnd;
  2206. error = do_path_lookup(olddfd, oldname, LOOKUP_PARENT, &oldnd);
  2207. if (error)
  2208. goto exit;
  2209. error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
  2210. if (error)
  2211. goto exit1;
  2212. error = -EXDEV;
  2213. if (oldnd.mnt != newnd.mnt)
  2214. goto exit2;
  2215. old_dir = oldnd.dentry;
  2216. error = -EBUSY;
  2217. if (oldnd.last_type != LAST_NORM)
  2218. goto exit2;
  2219. new_dir = newnd.dentry;
  2220. if (newnd.last_type != LAST_NORM)
  2221. goto exit2;
  2222. trap = lock_rename(new_dir, old_dir);
  2223. old_dentry = lookup_hash(&oldnd);
  2224. error = PTR_ERR(old_dentry);
  2225. if (IS_ERR(old_dentry))
  2226. goto exit3;
  2227. /* source must exist */
  2228. error = -ENOENT;
  2229. if (!old_dentry->d_inode)
  2230. goto exit4;
  2231. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2232. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2233. error = -ENOTDIR;
  2234. if (oldnd.last.name[oldnd.last.len])
  2235. goto exit4;
  2236. if (newnd.last.name[newnd.last.len])
  2237. goto exit4;
  2238. }
  2239. /* source should not be ancestor of target */
  2240. error = -EINVAL;
  2241. if (old_dentry == trap)
  2242. goto exit4;
  2243. new_dentry = lookup_hash(&newnd);
  2244. error = PTR_ERR(new_dentry);
  2245. if (IS_ERR(new_dentry))
  2246. goto exit4;
  2247. /* target should not be an ancestor of source */
  2248. error = -ENOTEMPTY;
  2249. if (new_dentry == trap)
  2250. goto exit5;
  2251. error = vfs_rename(old_dir->d_inode, old_dentry,
  2252. new_dir->d_inode, new_dentry);
  2253. exit5:
  2254. dput(new_dentry);
  2255. exit4:
  2256. dput(old_dentry);
  2257. exit3:
  2258. unlock_rename(new_dir, old_dir);
  2259. exit2:
  2260. path_release(&newnd);
  2261. exit1:
  2262. path_release(&oldnd);
  2263. exit:
  2264. return error;
  2265. }
  2266. asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
  2267. int newdfd, const char __user *newname)
  2268. {
  2269. int error;
  2270. char * from;
  2271. char * to;
  2272. from = getname(oldname);
  2273. if(IS_ERR(from))
  2274. return PTR_ERR(from);
  2275. to = getname(newname);
  2276. error = PTR_ERR(to);
  2277. if (!IS_ERR(to)) {
  2278. error = do_rename(olddfd, from, newdfd, to);
  2279. putname(to);
  2280. }
  2281. putname(from);
  2282. return error;
  2283. }
  2284. asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
  2285. {
  2286. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2287. }
  2288. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2289. {
  2290. int len;
  2291. len = PTR_ERR(link);
  2292. if (IS_ERR(link))
  2293. goto out;
  2294. len = strlen(link);
  2295. if (len > (unsigned) buflen)
  2296. len = buflen;
  2297. if (copy_to_user(buffer, link, len))
  2298. len = -EFAULT;
  2299. out:
  2300. return len;
  2301. }
  2302. /*
  2303. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2304. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2305. * using) it for any given inode is up to filesystem.
  2306. */
  2307. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2308. {
  2309. struct nameidata nd;
  2310. void *cookie;
  2311. nd.depth = 0;
  2312. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2313. if (!IS_ERR(cookie)) {
  2314. int res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2315. if (dentry->d_inode->i_op->put_link)
  2316. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2317. cookie = ERR_PTR(res);
  2318. }
  2319. return PTR_ERR(cookie);
  2320. }
  2321. int vfs_follow_link(struct nameidata *nd, const char *link)
  2322. {
  2323. return __vfs_follow_link(nd, link);
  2324. }
  2325. /* get the link contents into pagecache */
  2326. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2327. {
  2328. struct page * page;
  2329. struct address_space *mapping = dentry->d_inode->i_mapping;
  2330. page = read_mapping_page(mapping, 0, NULL);
  2331. if (IS_ERR(page))
  2332. goto sync_fail;
  2333. wait_on_page_locked(page);
  2334. if (!PageUptodate(page))
  2335. goto async_fail;
  2336. *ppage = page;
  2337. return kmap(page);
  2338. async_fail:
  2339. page_cache_release(page);
  2340. return ERR_PTR(-EIO);
  2341. sync_fail:
  2342. return (char*)page;
  2343. }
  2344. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2345. {
  2346. struct page *page = NULL;
  2347. char *s = page_getlink(dentry, &page);
  2348. int res = vfs_readlink(dentry,buffer,buflen,s);
  2349. if (page) {
  2350. kunmap(page);
  2351. page_cache_release(page);
  2352. }
  2353. return res;
  2354. }
  2355. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2356. {
  2357. struct page *page = NULL;
  2358. nd_set_link(nd, page_getlink(dentry, &page));
  2359. return page;
  2360. }
  2361. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2362. {
  2363. struct page *page = cookie;
  2364. if (page) {
  2365. kunmap(page);
  2366. page_cache_release(page);
  2367. }
  2368. }
  2369. int __page_symlink(struct inode *inode, const char *symname, int len,
  2370. gfp_t gfp_mask)
  2371. {
  2372. struct address_space *mapping = inode->i_mapping;
  2373. struct page *page;
  2374. int err = -ENOMEM;
  2375. char *kaddr;
  2376. retry:
  2377. page = find_or_create_page(mapping, 0, gfp_mask);
  2378. if (!page)
  2379. goto fail;
  2380. err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
  2381. if (err == AOP_TRUNCATED_PAGE) {
  2382. page_cache_release(page);
  2383. goto retry;
  2384. }
  2385. if (err)
  2386. goto fail_map;
  2387. kaddr = kmap_atomic(page, KM_USER0);
  2388. memcpy(kaddr, symname, len-1);
  2389. kunmap_atomic(kaddr, KM_USER0);
  2390. err = mapping->a_ops->commit_write(NULL, page, 0, len-1);
  2391. if (err == AOP_TRUNCATED_PAGE) {
  2392. page_cache_release(page);
  2393. goto retry;
  2394. }
  2395. if (err)
  2396. goto fail_map;
  2397. /*
  2398. * Notice that we are _not_ going to block here - end of page is
  2399. * unmapped, so this will only try to map the rest of page, see
  2400. * that it is unmapped (typically even will not look into inode -
  2401. * ->i_size will be enough for everything) and zero it out.
  2402. * OTOH it's obviously correct and should make the page up-to-date.
  2403. */
  2404. if (!PageUptodate(page)) {
  2405. err = mapping->a_ops->readpage(NULL, page);
  2406. if (err != AOP_TRUNCATED_PAGE)
  2407. wait_on_page_locked(page);
  2408. } else {
  2409. unlock_page(page);
  2410. }
  2411. page_cache_release(page);
  2412. if (err < 0)
  2413. goto fail;
  2414. mark_inode_dirty(inode);
  2415. return 0;
  2416. fail_map:
  2417. unlock_page(page);
  2418. page_cache_release(page);
  2419. fail:
  2420. return err;
  2421. }
  2422. int page_symlink(struct inode *inode, const char *symname, int len)
  2423. {
  2424. return __page_symlink(inode, symname, len,
  2425. mapping_gfp_mask(inode->i_mapping));
  2426. }
  2427. struct inode_operations page_symlink_inode_operations = {
  2428. .readlink = generic_readlink,
  2429. .follow_link = page_follow_link_light,
  2430. .put_link = page_put_link,
  2431. };
  2432. EXPORT_SYMBOL(__user_walk);
  2433. EXPORT_SYMBOL(__user_walk_fd);
  2434. EXPORT_SYMBOL(follow_down);
  2435. EXPORT_SYMBOL(follow_up);
  2436. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2437. EXPORT_SYMBOL(getname);
  2438. EXPORT_SYMBOL(lock_rename);
  2439. EXPORT_SYMBOL(lookup_one_len);
  2440. EXPORT_SYMBOL(page_follow_link_light);
  2441. EXPORT_SYMBOL(page_put_link);
  2442. EXPORT_SYMBOL(page_readlink);
  2443. EXPORT_SYMBOL(__page_symlink);
  2444. EXPORT_SYMBOL(page_symlink);
  2445. EXPORT_SYMBOL(page_symlink_inode_operations);
  2446. EXPORT_SYMBOL(path_lookup);
  2447. EXPORT_SYMBOL(path_release);
  2448. EXPORT_SYMBOL(path_walk);
  2449. EXPORT_SYMBOL(permission);
  2450. EXPORT_SYMBOL(vfs_permission);
  2451. EXPORT_SYMBOL(file_permission);
  2452. EXPORT_SYMBOL(unlock_rename);
  2453. EXPORT_SYMBOL(vfs_create);
  2454. EXPORT_SYMBOL(vfs_follow_link);
  2455. EXPORT_SYMBOL(vfs_link);
  2456. EXPORT_SYMBOL(vfs_mkdir);
  2457. EXPORT_SYMBOL(vfs_mknod);
  2458. EXPORT_SYMBOL(generic_permission);
  2459. EXPORT_SYMBOL(vfs_readlink);
  2460. EXPORT_SYMBOL(vfs_rename);
  2461. EXPORT_SYMBOL(vfs_rmdir);
  2462. EXPORT_SYMBOL(vfs_symlink);
  2463. EXPORT_SYMBOL(vfs_unlink);
  2464. EXPORT_SYMBOL(dentry_unhash);
  2465. EXPORT_SYMBOL(generic_readlink);