namei.c 88 KB

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