namei.c 86 KB

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