namei.c 70 KB

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