namei.c 70 KB

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