namei.c 71 KB

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