namei.c 83 KB

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