namei.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505
  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 (current_user_ns() != inode_userns(inode))
  199. goto other_perms;
  200. if (likely(current_fsuid() == inode->i_uid))
  201. mode >>= 6;
  202. else {
  203. if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
  204. int error = check_acl(inode, mask);
  205. if (error != -EAGAIN)
  206. return error;
  207. }
  208. if (in_group_p(inode->i_gid))
  209. mode >>= 3;
  210. }
  211. other_perms:
  212. /*
  213. * If the DACs are ok we don't need any capability check.
  214. */
  215. if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  216. return 0;
  217. return -EACCES;
  218. }
  219. /**
  220. * generic_permission - check for access rights on a Posix-like filesystem
  221. * @inode: inode to check access rights for
  222. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
  223. *
  224. * Used to check for read/write/execute permissions on a file.
  225. * We use "fsuid" for this, letting us set arbitrary permissions
  226. * for filesystem access without changing the "normal" uids which
  227. * are used for other things.
  228. *
  229. * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
  230. * request cannot be satisfied (eg. requires blocking or too much complexity).
  231. * It would then be called again in ref-walk mode.
  232. */
  233. int generic_permission(struct inode *inode, int mask)
  234. {
  235. int ret;
  236. /*
  237. * Do the basic permission checks.
  238. */
  239. ret = acl_permission_check(inode, mask);
  240. if (ret != -EACCES)
  241. return ret;
  242. if (S_ISDIR(inode->i_mode)) {
  243. /* DACs are overridable for directories */
  244. if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
  245. return 0;
  246. if (!(mask & MAY_WRITE))
  247. if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
  248. return 0;
  249. return -EACCES;
  250. }
  251. /*
  252. * Read/write DACs are always overridable.
  253. * Executable DACs are overridable when there is
  254. * at least one exec bit set.
  255. */
  256. if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
  257. if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
  258. return 0;
  259. /*
  260. * Searching includes executable on directories, else just read.
  261. */
  262. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  263. if (mask == MAY_READ)
  264. if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
  265. return 0;
  266. return -EACCES;
  267. }
  268. /*
  269. * We _really_ want to just do "generic_permission()" without
  270. * even looking at the inode->i_op values. So we keep a cache
  271. * flag in inode->i_opflags, that says "this has not special
  272. * permission function, use the fast case".
  273. */
  274. static inline int do_inode_permission(struct inode *inode, int mask)
  275. {
  276. if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
  277. if (likely(inode->i_op->permission))
  278. return inode->i_op->permission(inode, mask);
  279. /* This gets set once for the inode lifetime */
  280. spin_lock(&inode->i_lock);
  281. inode->i_opflags |= IOP_FASTPERM;
  282. spin_unlock(&inode->i_lock);
  283. }
  284. return generic_permission(inode, mask);
  285. }
  286. /**
  287. * inode_permission - check for access rights to a given inode
  288. * @inode: inode to check permission on
  289. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
  290. *
  291. * Used to check for read/write/execute permissions on an inode.
  292. * We use "fsuid" for this, letting us set arbitrary permissions
  293. * for filesystem access without changing the "normal" uids which
  294. * are used for other things.
  295. *
  296. * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
  297. */
  298. int inode_permission(struct inode *inode, int mask)
  299. {
  300. int retval;
  301. if (unlikely(mask & MAY_WRITE)) {
  302. umode_t mode = inode->i_mode;
  303. /*
  304. * Nobody gets write access to a read-only fs.
  305. */
  306. if (IS_RDONLY(inode) &&
  307. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  308. return -EROFS;
  309. /*
  310. * Nobody gets write access to an immutable file.
  311. */
  312. if (IS_IMMUTABLE(inode))
  313. return -EACCES;
  314. }
  315. retval = do_inode_permission(inode, mask);
  316. if (retval)
  317. return retval;
  318. retval = devcgroup_inode_permission(inode, mask);
  319. if (retval)
  320. return retval;
  321. return security_inode_permission(inode, mask);
  322. }
  323. /**
  324. * path_get - get a reference to a path
  325. * @path: path to get the reference to
  326. *
  327. * Given a path increment the reference count to the dentry and the vfsmount.
  328. */
  329. void path_get(struct path *path)
  330. {
  331. mntget(path->mnt);
  332. dget(path->dentry);
  333. }
  334. EXPORT_SYMBOL(path_get);
  335. /**
  336. * path_put - put a reference to a path
  337. * @path: path to put the reference to
  338. *
  339. * Given a path decrement the reference count to the dentry and the vfsmount.
  340. */
  341. void path_put(struct path *path)
  342. {
  343. dput(path->dentry);
  344. mntput(path->mnt);
  345. }
  346. EXPORT_SYMBOL(path_put);
  347. /*
  348. * Path walking has 2 modes, rcu-walk and ref-walk (see
  349. * Documentation/filesystems/path-lookup.txt). In situations when we can't
  350. * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
  351. * normal reference counts on dentries and vfsmounts to transition to rcu-walk
  352. * mode. Refcounts are grabbed at the last known good point before rcu-walk
  353. * got stuck, so ref-walk may continue from there. If this is not successful
  354. * (eg. a seqcount has changed), then failure is returned and it's up to caller
  355. * to restart the path walk from the beginning in ref-walk mode.
  356. */
  357. /**
  358. * unlazy_walk - try to switch to ref-walk mode.
  359. * @nd: nameidata pathwalk data
  360. * @dentry: child of nd->path.dentry or NULL
  361. * Returns: 0 on success, -ECHILD on failure
  362. *
  363. * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
  364. * for ref-walk mode. @dentry must be a path found by a do_lookup call on
  365. * @nd or NULL. Must be called from rcu-walk context.
  366. */
  367. static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
  368. {
  369. struct fs_struct *fs = current->fs;
  370. struct dentry *parent = nd->path.dentry;
  371. int want_root = 0;
  372. BUG_ON(!(nd->flags & LOOKUP_RCU));
  373. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  374. want_root = 1;
  375. spin_lock(&fs->lock);
  376. if (nd->root.mnt != fs->root.mnt ||
  377. nd->root.dentry != fs->root.dentry)
  378. goto err_root;
  379. }
  380. spin_lock(&parent->d_lock);
  381. if (!dentry) {
  382. if (!__d_rcu_to_refcount(parent, nd->seq))
  383. goto err_parent;
  384. BUG_ON(nd->inode != parent->d_inode);
  385. } else {
  386. if (dentry->d_parent != parent)
  387. goto err_parent;
  388. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  389. if (!__d_rcu_to_refcount(dentry, nd->seq))
  390. goto err_child;
  391. /*
  392. * If the sequence check on the child dentry passed, then
  393. * the child has not been removed from its parent. This
  394. * means the parent dentry must be valid and able to take
  395. * a reference at this point.
  396. */
  397. BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
  398. BUG_ON(!parent->d_count);
  399. parent->d_count++;
  400. spin_unlock(&dentry->d_lock);
  401. }
  402. spin_unlock(&parent->d_lock);
  403. if (want_root) {
  404. path_get(&nd->root);
  405. spin_unlock(&fs->lock);
  406. }
  407. mntget(nd->path.mnt);
  408. rcu_read_unlock();
  409. br_read_unlock(vfsmount_lock);
  410. nd->flags &= ~LOOKUP_RCU;
  411. return 0;
  412. err_child:
  413. spin_unlock(&dentry->d_lock);
  414. err_parent:
  415. spin_unlock(&parent->d_lock);
  416. err_root:
  417. if (want_root)
  418. spin_unlock(&fs->lock);
  419. return -ECHILD;
  420. }
  421. /**
  422. * release_open_intent - free up open intent resources
  423. * @nd: pointer to nameidata
  424. */
  425. void release_open_intent(struct nameidata *nd)
  426. {
  427. struct file *file = nd->intent.open.file;
  428. if (file && !IS_ERR(file)) {
  429. if (file->f_path.dentry == NULL)
  430. put_filp(file);
  431. else
  432. fput(file);
  433. }
  434. }
  435. static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
  436. {
  437. return dentry->d_op->d_revalidate(dentry, nd);
  438. }
  439. /**
  440. * complete_walk - successful completion of path walk
  441. * @nd: pointer nameidata
  442. *
  443. * If we had been in RCU mode, drop out of it and legitimize nd->path.
  444. * Revalidate the final result, unless we'd already done that during
  445. * the path walk or the filesystem doesn't ask for it. Return 0 on
  446. * success, -error on failure. In case of failure caller does not
  447. * need to drop nd->path.
  448. */
  449. static int complete_walk(struct nameidata *nd)
  450. {
  451. struct dentry *dentry = nd->path.dentry;
  452. int status;
  453. if (nd->flags & LOOKUP_RCU) {
  454. nd->flags &= ~LOOKUP_RCU;
  455. if (!(nd->flags & LOOKUP_ROOT))
  456. nd->root.mnt = NULL;
  457. spin_lock(&dentry->d_lock);
  458. if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
  459. spin_unlock(&dentry->d_lock);
  460. rcu_read_unlock();
  461. br_read_unlock(vfsmount_lock);
  462. return -ECHILD;
  463. }
  464. BUG_ON(nd->inode != dentry->d_inode);
  465. spin_unlock(&dentry->d_lock);
  466. mntget(nd->path.mnt);
  467. rcu_read_unlock();
  468. br_read_unlock(vfsmount_lock);
  469. }
  470. if (likely(!(nd->flags & LOOKUP_JUMPED)))
  471. return 0;
  472. if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
  473. return 0;
  474. if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
  475. return 0;
  476. /* Note: we do not d_invalidate() */
  477. status = d_revalidate(dentry, nd);
  478. if (status > 0)
  479. return 0;
  480. if (!status)
  481. status = -ESTALE;
  482. path_put(&nd->path);
  483. return status;
  484. }
  485. static __always_inline void set_root(struct nameidata *nd)
  486. {
  487. if (!nd->root.mnt)
  488. get_fs_root(current->fs, &nd->root);
  489. }
  490. static int link_path_walk(const char *, struct nameidata *);
  491. static __always_inline void set_root_rcu(struct nameidata *nd)
  492. {
  493. if (!nd->root.mnt) {
  494. struct fs_struct *fs = current->fs;
  495. unsigned seq;
  496. do {
  497. seq = read_seqcount_begin(&fs->seq);
  498. nd->root = fs->root;
  499. nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
  500. } while (read_seqcount_retry(&fs->seq, seq));
  501. }
  502. }
  503. static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
  504. {
  505. int ret;
  506. if (IS_ERR(link))
  507. goto fail;
  508. if (*link == '/') {
  509. set_root(nd);
  510. path_put(&nd->path);
  511. nd->path = nd->root;
  512. path_get(&nd->root);
  513. nd->flags |= LOOKUP_JUMPED;
  514. }
  515. nd->inode = nd->path.dentry->d_inode;
  516. ret = link_path_walk(link, nd);
  517. return ret;
  518. fail:
  519. path_put(&nd->path);
  520. return PTR_ERR(link);
  521. }
  522. static void path_put_conditional(struct path *path, struct nameidata *nd)
  523. {
  524. dput(path->dentry);
  525. if (path->mnt != nd->path.mnt)
  526. mntput(path->mnt);
  527. }
  528. static inline void path_to_nameidata(const struct path *path,
  529. struct nameidata *nd)
  530. {
  531. if (!(nd->flags & LOOKUP_RCU)) {
  532. dput(nd->path.dentry);
  533. if (nd->path.mnt != path->mnt)
  534. mntput(nd->path.mnt);
  535. }
  536. nd->path.mnt = path->mnt;
  537. nd->path.dentry = path->dentry;
  538. }
  539. static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
  540. {
  541. struct inode *inode = link->dentry->d_inode;
  542. if (!IS_ERR(cookie) && inode->i_op->put_link)
  543. inode->i_op->put_link(link->dentry, nd, cookie);
  544. path_put(link);
  545. }
  546. static __always_inline int
  547. follow_link(struct path *link, struct nameidata *nd, void **p)
  548. {
  549. int error;
  550. struct dentry *dentry = link->dentry;
  551. BUG_ON(nd->flags & LOOKUP_RCU);
  552. if (link->mnt == nd->path.mnt)
  553. mntget(link->mnt);
  554. if (unlikely(current->total_link_count >= 40)) {
  555. *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
  556. path_put(&nd->path);
  557. return -ELOOP;
  558. }
  559. cond_resched();
  560. current->total_link_count++;
  561. touch_atime(link);
  562. nd_set_link(nd, NULL);
  563. error = security_inode_follow_link(link->dentry, nd);
  564. if (error) {
  565. *p = ERR_PTR(error); /* no ->put_link(), please */
  566. path_put(&nd->path);
  567. return error;
  568. }
  569. nd->last_type = LAST_BIND;
  570. *p = dentry->d_inode->i_op->follow_link(dentry, nd);
  571. error = PTR_ERR(*p);
  572. if (!IS_ERR(*p)) {
  573. char *s = nd_get_link(nd);
  574. error = 0;
  575. if (s)
  576. error = __vfs_follow_link(nd, s);
  577. else if (nd->last_type == LAST_BIND) {
  578. nd->flags |= LOOKUP_JUMPED;
  579. nd->inode = nd->path.dentry->d_inode;
  580. if (nd->inode->i_op->follow_link) {
  581. /* stepped on a _really_ weird one */
  582. path_put(&nd->path);
  583. error = -ELOOP;
  584. }
  585. }
  586. }
  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 do_lookup(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. done:
  1082. path->mnt = mnt;
  1083. path->dentry = dentry;
  1084. err = follow_managed(path, nd->flags);
  1085. if (unlikely(err < 0)) {
  1086. path_put_conditional(path, nd);
  1087. return err;
  1088. }
  1089. if (err)
  1090. nd->flags |= LOOKUP_JUMPED;
  1091. *inode = path->dentry->d_inode;
  1092. return 0;
  1093. need_lookup:
  1094. BUG_ON(nd->inode != parent->d_inode);
  1095. mutex_lock(&parent->d_inode->i_mutex);
  1096. dentry = __lookup_hash(name, parent, nd);
  1097. mutex_unlock(&parent->d_inode->i_mutex);
  1098. if (IS_ERR(dentry))
  1099. return PTR_ERR(dentry);
  1100. goto done;
  1101. }
  1102. static inline int may_lookup(struct nameidata *nd)
  1103. {
  1104. if (nd->flags & LOOKUP_RCU) {
  1105. int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
  1106. if (err != -ECHILD)
  1107. return err;
  1108. if (unlazy_walk(nd, NULL))
  1109. return -ECHILD;
  1110. }
  1111. return inode_permission(nd->inode, MAY_EXEC);
  1112. }
  1113. static inline int handle_dots(struct nameidata *nd, int type)
  1114. {
  1115. if (type == LAST_DOTDOT) {
  1116. if (nd->flags & LOOKUP_RCU) {
  1117. if (follow_dotdot_rcu(nd))
  1118. return -ECHILD;
  1119. } else
  1120. follow_dotdot(nd);
  1121. }
  1122. return 0;
  1123. }
  1124. static void terminate_walk(struct nameidata *nd)
  1125. {
  1126. if (!(nd->flags & LOOKUP_RCU)) {
  1127. path_put(&nd->path);
  1128. } else {
  1129. nd->flags &= ~LOOKUP_RCU;
  1130. if (!(nd->flags & LOOKUP_ROOT))
  1131. nd->root.mnt = NULL;
  1132. rcu_read_unlock();
  1133. br_read_unlock(vfsmount_lock);
  1134. }
  1135. }
  1136. /*
  1137. * Do we need to follow links? We _really_ want to be able
  1138. * to do this check without having to look at inode->i_op,
  1139. * so we keep a cache of "no, this doesn't need follow_link"
  1140. * for the common case.
  1141. */
  1142. static inline int should_follow_link(struct inode *inode, int follow)
  1143. {
  1144. if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
  1145. if (likely(inode->i_op->follow_link))
  1146. return follow;
  1147. /* This gets set once for the inode lifetime */
  1148. spin_lock(&inode->i_lock);
  1149. inode->i_opflags |= IOP_NOFOLLOW;
  1150. spin_unlock(&inode->i_lock);
  1151. }
  1152. return 0;
  1153. }
  1154. static inline int walk_component(struct nameidata *nd, struct path *path,
  1155. struct qstr *name, int type, int follow)
  1156. {
  1157. struct inode *inode;
  1158. int err;
  1159. /*
  1160. * "." and ".." are special - ".." especially so because it has
  1161. * to be able to know about the current root directory and
  1162. * parent relationships.
  1163. */
  1164. if (unlikely(type != LAST_NORM))
  1165. return handle_dots(nd, type);
  1166. err = do_lookup(nd, name, path, &inode);
  1167. if (unlikely(err)) {
  1168. terminate_walk(nd);
  1169. return err;
  1170. }
  1171. if (!inode) {
  1172. path_to_nameidata(path, nd);
  1173. terminate_walk(nd);
  1174. return -ENOENT;
  1175. }
  1176. if (should_follow_link(inode, follow)) {
  1177. if (nd->flags & LOOKUP_RCU) {
  1178. if (unlikely(unlazy_walk(nd, path->dentry))) {
  1179. terminate_walk(nd);
  1180. return -ECHILD;
  1181. }
  1182. }
  1183. BUG_ON(inode != path->dentry->d_inode);
  1184. return 1;
  1185. }
  1186. path_to_nameidata(path, nd);
  1187. nd->inode = inode;
  1188. return 0;
  1189. }
  1190. /*
  1191. * This limits recursive symlink follows to 8, while
  1192. * limiting consecutive symlinks to 40.
  1193. *
  1194. * Without that kind of total limit, nasty chains of consecutive
  1195. * symlinks can cause almost arbitrarily long lookups.
  1196. */
  1197. static inline int nested_symlink(struct path *path, struct nameidata *nd)
  1198. {
  1199. int res;
  1200. if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
  1201. path_put_conditional(path, nd);
  1202. path_put(&nd->path);
  1203. return -ELOOP;
  1204. }
  1205. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  1206. nd->depth++;
  1207. current->link_count++;
  1208. do {
  1209. struct path link = *path;
  1210. void *cookie;
  1211. res = follow_link(&link, nd, &cookie);
  1212. if (!res)
  1213. res = walk_component(nd, path, &nd->last,
  1214. nd->last_type, LOOKUP_FOLLOW);
  1215. put_link(nd, &link, cookie);
  1216. } while (res > 0);
  1217. current->link_count--;
  1218. nd->depth--;
  1219. return res;
  1220. }
  1221. /*
  1222. * We really don't want to look at inode->i_op->lookup
  1223. * when we don't have to. So we keep a cache bit in
  1224. * the inode ->i_opflags field that says "yes, we can
  1225. * do lookup on this inode".
  1226. */
  1227. static inline int can_lookup(struct inode *inode)
  1228. {
  1229. if (likely(inode->i_opflags & IOP_LOOKUP))
  1230. return 1;
  1231. if (likely(!inode->i_op->lookup))
  1232. return 0;
  1233. /* We do this once for the lifetime of the inode */
  1234. spin_lock(&inode->i_lock);
  1235. inode->i_opflags |= IOP_LOOKUP;
  1236. spin_unlock(&inode->i_lock);
  1237. return 1;
  1238. }
  1239. /*
  1240. * We can do the critical dentry name comparison and hashing
  1241. * operations one word at a time, but we are limited to:
  1242. *
  1243. * - Architectures with fast unaligned word accesses. We could
  1244. * do a "get_unaligned()" if this helps and is sufficiently
  1245. * fast.
  1246. *
  1247. * - Little-endian machines (so that we can generate the mask
  1248. * of low bytes efficiently). Again, we *could* do a byte
  1249. * swapping load on big-endian architectures if that is not
  1250. * expensive enough to make the optimization worthless.
  1251. *
  1252. * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
  1253. * do not trap on the (extremely unlikely) case of a page
  1254. * crossing operation.
  1255. *
  1256. * - Furthermore, we need an efficient 64-bit compile for the
  1257. * 64-bit case in order to generate the "number of bytes in
  1258. * the final mask". Again, that could be replaced with a
  1259. * efficient population count instruction or similar.
  1260. */
  1261. #ifdef CONFIG_DCACHE_WORD_ACCESS
  1262. #include <asm/word-at-a-time.h>
  1263. #ifdef CONFIG_64BIT
  1264. static inline unsigned int fold_hash(unsigned long hash)
  1265. {
  1266. hash += hash >> (8*sizeof(int));
  1267. return hash;
  1268. }
  1269. #else /* 32-bit case */
  1270. #define fold_hash(x) (x)
  1271. #endif
  1272. unsigned int full_name_hash(const unsigned char *name, unsigned int len)
  1273. {
  1274. unsigned long a, mask;
  1275. unsigned long hash = 0;
  1276. for (;;) {
  1277. a = load_unaligned_zeropad(name);
  1278. if (len < sizeof(unsigned long))
  1279. break;
  1280. hash += a;
  1281. hash *= 9;
  1282. name += sizeof(unsigned long);
  1283. len -= sizeof(unsigned long);
  1284. if (!len)
  1285. goto done;
  1286. }
  1287. mask = ~(~0ul << len*8);
  1288. hash += mask & a;
  1289. done:
  1290. return fold_hash(hash);
  1291. }
  1292. EXPORT_SYMBOL(full_name_hash);
  1293. /*
  1294. * Calculate the length and hash of the path component, and
  1295. * return the length of the component;
  1296. */
  1297. static inline unsigned long hash_name(const char *name, unsigned int *hashp)
  1298. {
  1299. unsigned long a, mask, hash, len;
  1300. hash = a = 0;
  1301. len = -sizeof(unsigned long);
  1302. do {
  1303. hash = (hash + a) * 9;
  1304. len += sizeof(unsigned long);
  1305. a = load_unaligned_zeropad(name+len);
  1306. /* Do we have any NUL or '/' bytes in this word? */
  1307. mask = has_zero(a) | has_zero(a ^ REPEAT_BYTE('/'));
  1308. } while (!mask);
  1309. /* The mask *below* the first high bit set */
  1310. mask = (mask - 1) & ~mask;
  1311. mask >>= 7;
  1312. hash += a & mask;
  1313. *hashp = fold_hash(hash);
  1314. return len + count_masked_bytes(mask);
  1315. }
  1316. #else
  1317. unsigned int full_name_hash(const unsigned char *name, unsigned int len)
  1318. {
  1319. unsigned long hash = init_name_hash();
  1320. while (len--)
  1321. hash = partial_name_hash(*name++, hash);
  1322. return end_name_hash(hash);
  1323. }
  1324. EXPORT_SYMBOL(full_name_hash);
  1325. /*
  1326. * We know there's a real path component here of at least
  1327. * one character.
  1328. */
  1329. static inline unsigned long hash_name(const char *name, unsigned int *hashp)
  1330. {
  1331. unsigned long hash = init_name_hash();
  1332. unsigned long len = 0, c;
  1333. c = (unsigned char)*name;
  1334. do {
  1335. len++;
  1336. hash = partial_name_hash(c, hash);
  1337. c = (unsigned char)name[len];
  1338. } while (c && c != '/');
  1339. *hashp = end_name_hash(hash);
  1340. return len;
  1341. }
  1342. #endif
  1343. /*
  1344. * Name resolution.
  1345. * This is the basic name resolution function, turning a pathname into
  1346. * the final dentry. We expect 'base' to be positive and a directory.
  1347. *
  1348. * Returns 0 and nd will have valid dentry and mnt on success.
  1349. * Returns error and drops reference to input namei data on failure.
  1350. */
  1351. static int link_path_walk(const char *name, struct nameidata *nd)
  1352. {
  1353. struct path next;
  1354. int err;
  1355. while (*name=='/')
  1356. name++;
  1357. if (!*name)
  1358. return 0;
  1359. /* At this point we know we have a real path component. */
  1360. for(;;) {
  1361. struct qstr this;
  1362. long len;
  1363. int type;
  1364. err = may_lookup(nd);
  1365. if (err)
  1366. break;
  1367. len = hash_name(name, &this.hash);
  1368. this.name = name;
  1369. this.len = len;
  1370. type = LAST_NORM;
  1371. if (name[0] == '.') switch (len) {
  1372. case 2:
  1373. if (name[1] == '.') {
  1374. type = LAST_DOTDOT;
  1375. nd->flags |= LOOKUP_JUMPED;
  1376. }
  1377. break;
  1378. case 1:
  1379. type = LAST_DOT;
  1380. }
  1381. if (likely(type == LAST_NORM)) {
  1382. struct dentry *parent = nd->path.dentry;
  1383. nd->flags &= ~LOOKUP_JUMPED;
  1384. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1385. err = parent->d_op->d_hash(parent, nd->inode,
  1386. &this);
  1387. if (err < 0)
  1388. break;
  1389. }
  1390. }
  1391. if (!name[len])
  1392. goto last_component;
  1393. /*
  1394. * If it wasn't NUL, we know it was '/'. Skip that
  1395. * slash, and continue until no more slashes.
  1396. */
  1397. do {
  1398. len++;
  1399. } while (unlikely(name[len] == '/'));
  1400. if (!name[len])
  1401. goto last_component;
  1402. name += len;
  1403. err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
  1404. if (err < 0)
  1405. return err;
  1406. if (err) {
  1407. err = nested_symlink(&next, nd);
  1408. if (err)
  1409. return err;
  1410. }
  1411. if (can_lookup(nd->inode))
  1412. continue;
  1413. err = -ENOTDIR;
  1414. break;
  1415. /* here ends the main loop */
  1416. last_component:
  1417. nd->last = this;
  1418. nd->last_type = type;
  1419. return 0;
  1420. }
  1421. terminate_walk(nd);
  1422. return err;
  1423. }
  1424. static int path_init(int dfd, const char *name, unsigned int flags,
  1425. struct nameidata *nd, struct file **fp)
  1426. {
  1427. int retval = 0;
  1428. int fput_needed;
  1429. struct file *file;
  1430. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1431. nd->flags = flags | LOOKUP_JUMPED;
  1432. nd->depth = 0;
  1433. if (flags & LOOKUP_ROOT) {
  1434. struct inode *inode = nd->root.dentry->d_inode;
  1435. if (*name) {
  1436. if (!inode->i_op->lookup)
  1437. return -ENOTDIR;
  1438. retval = inode_permission(inode, MAY_EXEC);
  1439. if (retval)
  1440. return retval;
  1441. }
  1442. nd->path = nd->root;
  1443. nd->inode = inode;
  1444. if (flags & LOOKUP_RCU) {
  1445. br_read_lock(vfsmount_lock);
  1446. rcu_read_lock();
  1447. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1448. } else {
  1449. path_get(&nd->path);
  1450. }
  1451. return 0;
  1452. }
  1453. nd->root.mnt = NULL;
  1454. if (*name=='/') {
  1455. if (flags & LOOKUP_RCU) {
  1456. br_read_lock(vfsmount_lock);
  1457. rcu_read_lock();
  1458. set_root_rcu(nd);
  1459. } else {
  1460. set_root(nd);
  1461. path_get(&nd->root);
  1462. }
  1463. nd->path = nd->root;
  1464. } else if (dfd == AT_FDCWD) {
  1465. if (flags & LOOKUP_RCU) {
  1466. struct fs_struct *fs = current->fs;
  1467. unsigned seq;
  1468. br_read_lock(vfsmount_lock);
  1469. rcu_read_lock();
  1470. do {
  1471. seq = read_seqcount_begin(&fs->seq);
  1472. nd->path = fs->pwd;
  1473. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1474. } while (read_seqcount_retry(&fs->seq, seq));
  1475. } else {
  1476. get_fs_pwd(current->fs, &nd->path);
  1477. }
  1478. } else {
  1479. struct dentry *dentry;
  1480. file = fget_raw_light(dfd, &fput_needed);
  1481. retval = -EBADF;
  1482. if (!file)
  1483. goto out_fail;
  1484. dentry = file->f_path.dentry;
  1485. if (*name) {
  1486. retval = -ENOTDIR;
  1487. if (!S_ISDIR(dentry->d_inode->i_mode))
  1488. goto fput_fail;
  1489. retval = inode_permission(dentry->d_inode, MAY_EXEC);
  1490. if (retval)
  1491. goto fput_fail;
  1492. }
  1493. nd->path = file->f_path;
  1494. if (flags & LOOKUP_RCU) {
  1495. if (fput_needed)
  1496. *fp = file;
  1497. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1498. br_read_lock(vfsmount_lock);
  1499. rcu_read_lock();
  1500. } else {
  1501. path_get(&file->f_path);
  1502. fput_light(file, fput_needed);
  1503. }
  1504. }
  1505. nd->inode = nd->path.dentry->d_inode;
  1506. return 0;
  1507. fput_fail:
  1508. fput_light(file, fput_needed);
  1509. out_fail:
  1510. return retval;
  1511. }
  1512. static inline int lookup_last(struct nameidata *nd, struct path *path)
  1513. {
  1514. if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
  1515. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1516. nd->flags &= ~LOOKUP_PARENT;
  1517. return walk_component(nd, path, &nd->last, nd->last_type,
  1518. nd->flags & LOOKUP_FOLLOW);
  1519. }
  1520. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1521. static int path_lookupat(int dfd, const char *name,
  1522. unsigned int flags, struct nameidata *nd)
  1523. {
  1524. struct file *base = NULL;
  1525. struct path path;
  1526. int err;
  1527. /*
  1528. * Path walking is largely split up into 2 different synchronisation
  1529. * schemes, rcu-walk and ref-walk (explained in
  1530. * Documentation/filesystems/path-lookup.txt). These share much of the
  1531. * path walk code, but some things particularly setup, cleanup, and
  1532. * following mounts are sufficiently divergent that functions are
  1533. * duplicated. Typically there is a function foo(), and its RCU
  1534. * analogue, foo_rcu().
  1535. *
  1536. * -ECHILD is the error number of choice (just to avoid clashes) that
  1537. * is returned if some aspect of an rcu-walk fails. Such an error must
  1538. * be handled by restarting a traditional ref-walk (which will always
  1539. * be able to complete).
  1540. */
  1541. err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
  1542. if (unlikely(err))
  1543. return err;
  1544. current->total_link_count = 0;
  1545. err = link_path_walk(name, nd);
  1546. if (!err && !(flags & LOOKUP_PARENT)) {
  1547. err = lookup_last(nd, &path);
  1548. while (err > 0) {
  1549. void *cookie;
  1550. struct path link = path;
  1551. nd->flags |= LOOKUP_PARENT;
  1552. err = follow_link(&link, nd, &cookie);
  1553. if (!err)
  1554. err = lookup_last(nd, &path);
  1555. put_link(nd, &link, cookie);
  1556. }
  1557. }
  1558. if (!err)
  1559. err = complete_walk(nd);
  1560. if (!err && nd->flags & LOOKUP_DIRECTORY) {
  1561. if (!nd->inode->i_op->lookup) {
  1562. path_put(&nd->path);
  1563. err = -ENOTDIR;
  1564. }
  1565. }
  1566. if (base)
  1567. fput(base);
  1568. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  1569. path_put(&nd->root);
  1570. nd->root.mnt = NULL;
  1571. }
  1572. return err;
  1573. }
  1574. static int do_path_lookup(int dfd, const char *name,
  1575. unsigned int flags, struct nameidata *nd)
  1576. {
  1577. int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
  1578. if (unlikely(retval == -ECHILD))
  1579. retval = path_lookupat(dfd, name, flags, nd);
  1580. if (unlikely(retval == -ESTALE))
  1581. retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
  1582. if (likely(!retval)) {
  1583. if (unlikely(!audit_dummy_context())) {
  1584. if (nd->path.dentry && nd->inode)
  1585. audit_inode(name, nd->path.dentry);
  1586. }
  1587. }
  1588. return retval;
  1589. }
  1590. int kern_path_parent(const char *name, struct nameidata *nd)
  1591. {
  1592. return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
  1593. }
  1594. int kern_path(const char *name, unsigned int flags, struct path *path)
  1595. {
  1596. struct nameidata nd;
  1597. int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  1598. if (!res)
  1599. *path = nd.path;
  1600. return res;
  1601. }
  1602. /**
  1603. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  1604. * @dentry: pointer to dentry of the base directory
  1605. * @mnt: pointer to vfs mount of the base directory
  1606. * @name: pointer to file name
  1607. * @flags: lookup flags
  1608. * @path: pointer to struct path to fill
  1609. */
  1610. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1611. const char *name, unsigned int flags,
  1612. struct path *path)
  1613. {
  1614. struct nameidata nd;
  1615. int err;
  1616. nd.root.dentry = dentry;
  1617. nd.root.mnt = mnt;
  1618. BUG_ON(flags & LOOKUP_PARENT);
  1619. /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
  1620. err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
  1621. if (!err)
  1622. *path = nd.path;
  1623. return err;
  1624. }
  1625. /*
  1626. * Restricted form of lookup. Doesn't follow links, single-component only,
  1627. * needs parent already locked. Doesn't follow mounts.
  1628. * SMP-safe.
  1629. */
  1630. static struct dentry *lookup_hash(struct nameidata *nd)
  1631. {
  1632. return __lookup_hash(&nd->last, nd->path.dentry, nd);
  1633. }
  1634. /**
  1635. * lookup_one_len - filesystem helper to lookup single pathname component
  1636. * @name: pathname component to lookup
  1637. * @base: base directory to lookup from
  1638. * @len: maximum length @len should be interpreted to
  1639. *
  1640. * Note that this routine is purely a helper for filesystem usage and should
  1641. * not be called by generic code. Also note that by using this function the
  1642. * nameidata argument is passed to the filesystem methods and a filesystem
  1643. * using this helper needs to be prepared for that.
  1644. */
  1645. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1646. {
  1647. struct qstr this;
  1648. unsigned int c;
  1649. int err;
  1650. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  1651. this.name = name;
  1652. this.len = len;
  1653. this.hash = full_name_hash(name, len);
  1654. if (!len)
  1655. return ERR_PTR(-EACCES);
  1656. while (len--) {
  1657. c = *(const unsigned char *)name++;
  1658. if (c == '/' || c == '\0')
  1659. return ERR_PTR(-EACCES);
  1660. }
  1661. /*
  1662. * See if the low-level filesystem might want
  1663. * to use its own hash..
  1664. */
  1665. if (base->d_flags & DCACHE_OP_HASH) {
  1666. int err = base->d_op->d_hash(base, base->d_inode, &this);
  1667. if (err < 0)
  1668. return ERR_PTR(err);
  1669. }
  1670. err = inode_permission(base->d_inode, MAY_EXEC);
  1671. if (err)
  1672. return ERR_PTR(err);
  1673. return __lookup_hash(&this, base, NULL);
  1674. }
  1675. int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
  1676. struct path *path, int *empty)
  1677. {
  1678. struct nameidata nd;
  1679. char *tmp = getname_flags(name, flags, empty);
  1680. int err = PTR_ERR(tmp);
  1681. if (!IS_ERR(tmp)) {
  1682. BUG_ON(flags & LOOKUP_PARENT);
  1683. err = do_path_lookup(dfd, tmp, flags, &nd);
  1684. putname(tmp);
  1685. if (!err)
  1686. *path = nd.path;
  1687. }
  1688. return err;
  1689. }
  1690. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1691. struct path *path)
  1692. {
  1693. return user_path_at_empty(dfd, name, flags, path, NULL);
  1694. }
  1695. static int user_path_parent(int dfd, const char __user *path,
  1696. struct nameidata *nd, char **name)
  1697. {
  1698. char *s = getname(path);
  1699. int error;
  1700. if (IS_ERR(s))
  1701. return PTR_ERR(s);
  1702. error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
  1703. if (error)
  1704. putname(s);
  1705. else
  1706. *name = s;
  1707. return error;
  1708. }
  1709. /*
  1710. * It's inline, so penalty for filesystems that don't use sticky bit is
  1711. * minimal.
  1712. */
  1713. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1714. {
  1715. uid_t fsuid = current_fsuid();
  1716. if (!(dir->i_mode & S_ISVTX))
  1717. return 0;
  1718. if (current_user_ns() != inode_userns(inode))
  1719. goto other_userns;
  1720. if (inode->i_uid == fsuid)
  1721. return 0;
  1722. if (dir->i_uid == fsuid)
  1723. return 0;
  1724. other_userns:
  1725. return !ns_capable(inode_userns(inode), CAP_FOWNER);
  1726. }
  1727. /*
  1728. * Check whether we can remove a link victim from directory dir, check
  1729. * whether the type of victim is right.
  1730. * 1. We can't do it if dir is read-only (done in permission())
  1731. * 2. We should have write and exec permissions on dir
  1732. * 3. We can't remove anything from append-only dir
  1733. * 4. We can't do anything with immutable dir (done in permission())
  1734. * 5. If the sticky bit on dir is set we should either
  1735. * a. be owner of dir, or
  1736. * b. be owner of victim, or
  1737. * c. have CAP_FOWNER capability
  1738. * 6. If the victim is append-only or immutable we can't do antyhing with
  1739. * links pointing to it.
  1740. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1741. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1742. * 9. We can't remove a root or mountpoint.
  1743. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1744. * nfs_async_unlink().
  1745. */
  1746. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1747. {
  1748. int error;
  1749. if (!victim->d_inode)
  1750. return -ENOENT;
  1751. BUG_ON(victim->d_parent->d_inode != dir);
  1752. audit_inode_child(victim, dir);
  1753. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1754. if (error)
  1755. return error;
  1756. if (IS_APPEND(dir))
  1757. return -EPERM;
  1758. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1759. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  1760. return -EPERM;
  1761. if (isdir) {
  1762. if (!S_ISDIR(victim->d_inode->i_mode))
  1763. return -ENOTDIR;
  1764. if (IS_ROOT(victim))
  1765. return -EBUSY;
  1766. } else if (S_ISDIR(victim->d_inode->i_mode))
  1767. return -EISDIR;
  1768. if (IS_DEADDIR(dir))
  1769. return -ENOENT;
  1770. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1771. return -EBUSY;
  1772. return 0;
  1773. }
  1774. /* Check whether we can create an object with dentry child in directory
  1775. * dir.
  1776. * 1. We can't do it if child already exists (open has special treatment for
  1777. * this case, but since we are inlined it's OK)
  1778. * 2. We can't do it if dir is read-only (done in permission())
  1779. * 3. We should have write and exec permissions on dir
  1780. * 4. We can't do it if dir is immutable (done in permission())
  1781. */
  1782. static inline int may_create(struct inode *dir, struct dentry *child)
  1783. {
  1784. if (child->d_inode)
  1785. return -EEXIST;
  1786. if (IS_DEADDIR(dir))
  1787. return -ENOENT;
  1788. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1789. }
  1790. /*
  1791. * p1 and p2 should be directories on the same fs.
  1792. */
  1793. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1794. {
  1795. struct dentry *p;
  1796. if (p1 == p2) {
  1797. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1798. return NULL;
  1799. }
  1800. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1801. p = d_ancestor(p2, p1);
  1802. if (p) {
  1803. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1804. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1805. return p;
  1806. }
  1807. p = d_ancestor(p1, p2);
  1808. if (p) {
  1809. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1810. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1811. return p;
  1812. }
  1813. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1814. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1815. return NULL;
  1816. }
  1817. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1818. {
  1819. mutex_unlock(&p1->d_inode->i_mutex);
  1820. if (p1 != p2) {
  1821. mutex_unlock(&p2->d_inode->i_mutex);
  1822. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1823. }
  1824. }
  1825. int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  1826. struct nameidata *nd)
  1827. {
  1828. int error = may_create(dir, dentry);
  1829. if (error)
  1830. return error;
  1831. if (!dir->i_op->create)
  1832. return -EACCES; /* shouldn't it be ENOSYS? */
  1833. mode &= S_IALLUGO;
  1834. mode |= S_IFREG;
  1835. error = security_inode_create(dir, dentry, mode);
  1836. if (error)
  1837. return error;
  1838. error = dir->i_op->create(dir, dentry, mode, nd);
  1839. if (!error)
  1840. fsnotify_create(dir, dentry);
  1841. return error;
  1842. }
  1843. static int may_open(struct path *path, int acc_mode, int flag)
  1844. {
  1845. struct dentry *dentry = path->dentry;
  1846. struct inode *inode = dentry->d_inode;
  1847. int error;
  1848. /* O_PATH? */
  1849. if (!acc_mode)
  1850. return 0;
  1851. if (!inode)
  1852. return -ENOENT;
  1853. switch (inode->i_mode & S_IFMT) {
  1854. case S_IFLNK:
  1855. return -ELOOP;
  1856. case S_IFDIR:
  1857. if (acc_mode & MAY_WRITE)
  1858. return -EISDIR;
  1859. break;
  1860. case S_IFBLK:
  1861. case S_IFCHR:
  1862. if (path->mnt->mnt_flags & MNT_NODEV)
  1863. return -EACCES;
  1864. /*FALLTHRU*/
  1865. case S_IFIFO:
  1866. case S_IFSOCK:
  1867. flag &= ~O_TRUNC;
  1868. break;
  1869. }
  1870. error = inode_permission(inode, acc_mode);
  1871. if (error)
  1872. return error;
  1873. /*
  1874. * An append-only file must be opened in append mode for writing.
  1875. */
  1876. if (IS_APPEND(inode)) {
  1877. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  1878. return -EPERM;
  1879. if (flag & O_TRUNC)
  1880. return -EPERM;
  1881. }
  1882. /* O_NOATIME can only be set by the owner or superuser */
  1883. if (flag & O_NOATIME && !inode_owner_or_capable(inode))
  1884. return -EPERM;
  1885. return 0;
  1886. }
  1887. static int handle_truncate(struct file *filp)
  1888. {
  1889. struct path *path = &filp->f_path;
  1890. struct inode *inode = path->dentry->d_inode;
  1891. int error = get_write_access(inode);
  1892. if (error)
  1893. return error;
  1894. /*
  1895. * Refuse to truncate files with mandatory locks held on them.
  1896. */
  1897. error = locks_verify_locked(inode);
  1898. if (!error)
  1899. error = security_path_truncate(path);
  1900. if (!error) {
  1901. error = do_truncate(path->dentry, 0,
  1902. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1903. filp);
  1904. }
  1905. put_write_access(inode);
  1906. return error;
  1907. }
  1908. static inline int open_to_namei_flags(int flag)
  1909. {
  1910. if ((flag & O_ACCMODE) == 3)
  1911. flag--;
  1912. return flag;
  1913. }
  1914. /*
  1915. * Handle the last step of open()
  1916. */
  1917. static struct file *do_last(struct nameidata *nd, struct path *path,
  1918. const struct open_flags *op, const char *pathname)
  1919. {
  1920. struct dentry *dir = nd->path.dentry;
  1921. struct dentry *dentry;
  1922. int open_flag = op->open_flag;
  1923. int will_truncate = open_flag & O_TRUNC;
  1924. int want_write = 0;
  1925. int acc_mode = op->acc_mode;
  1926. struct file *filp;
  1927. int error;
  1928. nd->flags &= ~LOOKUP_PARENT;
  1929. nd->flags |= op->intent;
  1930. switch (nd->last_type) {
  1931. case LAST_DOTDOT:
  1932. case LAST_DOT:
  1933. error = handle_dots(nd, nd->last_type);
  1934. if (error)
  1935. return ERR_PTR(error);
  1936. /* fallthrough */
  1937. case LAST_ROOT:
  1938. error = complete_walk(nd);
  1939. if (error)
  1940. return ERR_PTR(error);
  1941. audit_inode(pathname, nd->path.dentry);
  1942. if (open_flag & O_CREAT) {
  1943. error = -EISDIR;
  1944. goto exit;
  1945. }
  1946. goto ok;
  1947. case LAST_BIND:
  1948. error = complete_walk(nd);
  1949. if (error)
  1950. return ERR_PTR(error);
  1951. audit_inode(pathname, dir);
  1952. goto ok;
  1953. }
  1954. if (!(open_flag & O_CREAT)) {
  1955. int symlink_ok = 0;
  1956. if (nd->last.name[nd->last.len])
  1957. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1958. if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
  1959. symlink_ok = 1;
  1960. /* we _can_ be in RCU mode here */
  1961. error = walk_component(nd, path, &nd->last, LAST_NORM,
  1962. !symlink_ok);
  1963. if (error < 0)
  1964. return ERR_PTR(error);
  1965. if (error) /* symlink */
  1966. return NULL;
  1967. /* sayonara */
  1968. error = complete_walk(nd);
  1969. if (error)
  1970. return ERR_PTR(error);
  1971. error = -ENOTDIR;
  1972. if (nd->flags & LOOKUP_DIRECTORY) {
  1973. if (!nd->inode->i_op->lookup)
  1974. goto exit;
  1975. }
  1976. audit_inode(pathname, nd->path.dentry);
  1977. goto ok;
  1978. }
  1979. /* create side of things */
  1980. /*
  1981. * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED has been
  1982. * cleared when we got to the last component we are about to look up
  1983. */
  1984. error = complete_walk(nd);
  1985. if (error)
  1986. return ERR_PTR(error);
  1987. audit_inode(pathname, dir);
  1988. error = -EISDIR;
  1989. /* trailing slashes? */
  1990. if (nd->last.name[nd->last.len])
  1991. goto exit;
  1992. mutex_lock(&dir->d_inode->i_mutex);
  1993. dentry = lookup_hash(nd);
  1994. error = PTR_ERR(dentry);
  1995. if (IS_ERR(dentry)) {
  1996. mutex_unlock(&dir->d_inode->i_mutex);
  1997. goto exit;
  1998. }
  1999. path->dentry = dentry;
  2000. path->mnt = nd->path.mnt;
  2001. /* Negative dentry, just create the file */
  2002. if (!dentry->d_inode) {
  2003. umode_t mode = op->mode;
  2004. if (!IS_POSIXACL(dir->d_inode))
  2005. mode &= ~current_umask();
  2006. /*
  2007. * This write is needed to ensure that a
  2008. * rw->ro transition does not occur between
  2009. * the time when the file is created and when
  2010. * a permanent write count is taken through
  2011. * the 'struct file' in nameidata_to_filp().
  2012. */
  2013. error = mnt_want_write(nd->path.mnt);
  2014. if (error)
  2015. goto exit_mutex_unlock;
  2016. want_write = 1;
  2017. /* Don't check for write permission, don't truncate */
  2018. open_flag &= ~O_TRUNC;
  2019. will_truncate = 0;
  2020. acc_mode = MAY_OPEN;
  2021. error = security_path_mknod(&nd->path, dentry, mode, 0);
  2022. if (error)
  2023. goto exit_mutex_unlock;
  2024. error = vfs_create(dir->d_inode, dentry, mode, nd);
  2025. if (error)
  2026. goto exit_mutex_unlock;
  2027. mutex_unlock(&dir->d_inode->i_mutex);
  2028. dput(nd->path.dentry);
  2029. nd->path.dentry = dentry;
  2030. goto common;
  2031. }
  2032. /*
  2033. * It already exists.
  2034. */
  2035. mutex_unlock(&dir->d_inode->i_mutex);
  2036. audit_inode(pathname, path->dentry);
  2037. error = -EEXIST;
  2038. if (open_flag & O_EXCL)
  2039. goto exit_dput;
  2040. error = follow_managed(path, nd->flags);
  2041. if (error < 0)
  2042. goto exit_dput;
  2043. if (error)
  2044. nd->flags |= LOOKUP_JUMPED;
  2045. error = -ENOENT;
  2046. if (!path->dentry->d_inode)
  2047. goto exit_dput;
  2048. if (path->dentry->d_inode->i_op->follow_link)
  2049. return NULL;
  2050. path_to_nameidata(path, nd);
  2051. nd->inode = path->dentry->d_inode;
  2052. /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
  2053. error = complete_walk(nd);
  2054. if (error)
  2055. return ERR_PTR(error);
  2056. error = -EISDIR;
  2057. if (S_ISDIR(nd->inode->i_mode))
  2058. goto exit;
  2059. ok:
  2060. if (!S_ISREG(nd->inode->i_mode))
  2061. will_truncate = 0;
  2062. if (will_truncate) {
  2063. error = mnt_want_write(nd->path.mnt);
  2064. if (error)
  2065. goto exit;
  2066. want_write = 1;
  2067. }
  2068. common:
  2069. error = may_open(&nd->path, acc_mode, open_flag);
  2070. if (error)
  2071. goto exit;
  2072. filp = nameidata_to_filp(nd);
  2073. if (!IS_ERR(filp)) {
  2074. error = ima_file_check(filp, op->acc_mode);
  2075. if (error) {
  2076. fput(filp);
  2077. filp = ERR_PTR(error);
  2078. }
  2079. }
  2080. if (!IS_ERR(filp)) {
  2081. if (will_truncate) {
  2082. error = handle_truncate(filp);
  2083. if (error) {
  2084. fput(filp);
  2085. filp = ERR_PTR(error);
  2086. }
  2087. }
  2088. }
  2089. out:
  2090. if (want_write)
  2091. mnt_drop_write(nd->path.mnt);
  2092. path_put(&nd->path);
  2093. return filp;
  2094. exit_mutex_unlock:
  2095. mutex_unlock(&dir->d_inode->i_mutex);
  2096. exit_dput:
  2097. path_put_conditional(path, nd);
  2098. exit:
  2099. filp = ERR_PTR(error);
  2100. goto out;
  2101. }
  2102. static struct file *path_openat(int dfd, const char *pathname,
  2103. struct nameidata *nd, const struct open_flags *op, int flags)
  2104. {
  2105. struct file *base = NULL;
  2106. struct file *filp;
  2107. struct path path;
  2108. int error;
  2109. filp = get_empty_filp();
  2110. if (!filp)
  2111. return ERR_PTR(-ENFILE);
  2112. filp->f_flags = op->open_flag;
  2113. nd->intent.open.file = filp;
  2114. nd->intent.open.flags = open_to_namei_flags(op->open_flag);
  2115. nd->intent.open.create_mode = op->mode;
  2116. error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
  2117. if (unlikely(error))
  2118. goto out_filp;
  2119. current->total_link_count = 0;
  2120. error = link_path_walk(pathname, nd);
  2121. if (unlikely(error))
  2122. goto out_filp;
  2123. filp = do_last(nd, &path, op, pathname);
  2124. while (unlikely(!filp)) { /* trailing symlink */
  2125. struct path link = path;
  2126. void *cookie;
  2127. if (!(nd->flags & LOOKUP_FOLLOW)) {
  2128. path_put_conditional(&path, nd);
  2129. path_put(&nd->path);
  2130. filp = ERR_PTR(-ELOOP);
  2131. break;
  2132. }
  2133. nd->flags |= LOOKUP_PARENT;
  2134. nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  2135. error = follow_link(&link, nd, &cookie);
  2136. if (unlikely(error))
  2137. filp = ERR_PTR(error);
  2138. else
  2139. filp = do_last(nd, &path, op, pathname);
  2140. put_link(nd, &link, cookie);
  2141. }
  2142. out:
  2143. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
  2144. path_put(&nd->root);
  2145. if (base)
  2146. fput(base);
  2147. release_open_intent(nd);
  2148. return filp;
  2149. out_filp:
  2150. filp = ERR_PTR(error);
  2151. goto out;
  2152. }
  2153. struct file *do_filp_open(int dfd, const char *pathname,
  2154. const struct open_flags *op, int flags)
  2155. {
  2156. struct nameidata nd;
  2157. struct file *filp;
  2158. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
  2159. if (unlikely(filp == ERR_PTR(-ECHILD)))
  2160. filp = path_openat(dfd, pathname, &nd, op, flags);
  2161. if (unlikely(filp == ERR_PTR(-ESTALE)))
  2162. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
  2163. return filp;
  2164. }
  2165. struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  2166. const char *name, const struct open_flags *op, int flags)
  2167. {
  2168. struct nameidata nd;
  2169. struct file *file;
  2170. nd.root.mnt = mnt;
  2171. nd.root.dentry = dentry;
  2172. flags |= LOOKUP_ROOT;
  2173. if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
  2174. return ERR_PTR(-ELOOP);
  2175. file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
  2176. if (unlikely(file == ERR_PTR(-ECHILD)))
  2177. file = path_openat(-1, name, &nd, op, flags);
  2178. if (unlikely(file == ERR_PTR(-ESTALE)))
  2179. file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
  2180. return file;
  2181. }
  2182. struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
  2183. {
  2184. struct dentry *dentry = ERR_PTR(-EEXIST);
  2185. struct nameidata nd;
  2186. int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
  2187. if (error)
  2188. return ERR_PTR(error);
  2189. /*
  2190. * Yucky last component or no last component at all?
  2191. * (foo/., foo/.., /////)
  2192. */
  2193. if (nd.last_type != LAST_NORM)
  2194. goto out;
  2195. nd.flags &= ~LOOKUP_PARENT;
  2196. nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  2197. nd.intent.open.flags = O_EXCL;
  2198. /*
  2199. * Do the final lookup.
  2200. */
  2201. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2202. dentry = lookup_hash(&nd);
  2203. if (IS_ERR(dentry))
  2204. goto fail;
  2205. if (dentry->d_inode)
  2206. goto eexist;
  2207. /*
  2208. * Special case - lookup gave negative, but... we had foo/bar/
  2209. * From the vfs_mknod() POV we just have a negative dentry -
  2210. * all is fine. Let's be bastards - you had / on the end, you've
  2211. * been asking for (non-existent) directory. -ENOENT for you.
  2212. */
  2213. if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
  2214. dput(dentry);
  2215. dentry = ERR_PTR(-ENOENT);
  2216. goto fail;
  2217. }
  2218. *path = nd.path;
  2219. return dentry;
  2220. eexist:
  2221. dput(dentry);
  2222. dentry = ERR_PTR(-EEXIST);
  2223. fail:
  2224. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2225. out:
  2226. path_put(&nd.path);
  2227. return dentry;
  2228. }
  2229. EXPORT_SYMBOL(kern_path_create);
  2230. struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
  2231. {
  2232. char *tmp = getname(pathname);
  2233. struct dentry *res;
  2234. if (IS_ERR(tmp))
  2235. return ERR_CAST(tmp);
  2236. res = kern_path_create(dfd, tmp, path, is_dir);
  2237. putname(tmp);
  2238. return res;
  2239. }
  2240. EXPORT_SYMBOL(user_path_create);
  2241. int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  2242. {
  2243. int error = may_create(dir, dentry);
  2244. if (error)
  2245. return error;
  2246. if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
  2247. !ns_capable(inode_userns(dir), CAP_MKNOD))
  2248. return -EPERM;
  2249. if (!dir->i_op->mknod)
  2250. return -EPERM;
  2251. error = devcgroup_inode_mknod(mode, dev);
  2252. if (error)
  2253. return error;
  2254. error = security_inode_mknod(dir, dentry, mode, dev);
  2255. if (error)
  2256. return error;
  2257. error = dir->i_op->mknod(dir, dentry, mode, dev);
  2258. if (!error)
  2259. fsnotify_create(dir, dentry);
  2260. return error;
  2261. }
  2262. static int may_mknod(umode_t mode)
  2263. {
  2264. switch (mode & S_IFMT) {
  2265. case S_IFREG:
  2266. case S_IFCHR:
  2267. case S_IFBLK:
  2268. case S_IFIFO:
  2269. case S_IFSOCK:
  2270. case 0: /* zero mode translates to S_IFREG */
  2271. return 0;
  2272. case S_IFDIR:
  2273. return -EPERM;
  2274. default:
  2275. return -EINVAL;
  2276. }
  2277. }
  2278. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
  2279. unsigned, dev)
  2280. {
  2281. struct dentry *dentry;
  2282. struct path path;
  2283. int error;
  2284. if (S_ISDIR(mode))
  2285. return -EPERM;
  2286. dentry = user_path_create(dfd, filename, &path, 0);
  2287. if (IS_ERR(dentry))
  2288. return PTR_ERR(dentry);
  2289. if (!IS_POSIXACL(path.dentry->d_inode))
  2290. mode &= ~current_umask();
  2291. error = may_mknod(mode);
  2292. if (error)
  2293. goto out_dput;
  2294. error = mnt_want_write(path.mnt);
  2295. if (error)
  2296. goto out_dput;
  2297. error = security_path_mknod(&path, dentry, mode, dev);
  2298. if (error)
  2299. goto out_drop_write;
  2300. switch (mode & S_IFMT) {
  2301. case 0: case S_IFREG:
  2302. error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
  2303. break;
  2304. case S_IFCHR: case S_IFBLK:
  2305. error = vfs_mknod(path.dentry->d_inode,dentry,mode,
  2306. new_decode_dev(dev));
  2307. break;
  2308. case S_IFIFO: case S_IFSOCK:
  2309. error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
  2310. break;
  2311. }
  2312. out_drop_write:
  2313. mnt_drop_write(path.mnt);
  2314. out_dput:
  2315. dput(dentry);
  2316. mutex_unlock(&path.dentry->d_inode->i_mutex);
  2317. path_put(&path);
  2318. return error;
  2319. }
  2320. SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
  2321. {
  2322. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  2323. }
  2324. int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  2325. {
  2326. int error = may_create(dir, dentry);
  2327. unsigned max_links = dir->i_sb->s_max_links;
  2328. if (error)
  2329. return error;
  2330. if (!dir->i_op->mkdir)
  2331. return -EPERM;
  2332. mode &= (S_IRWXUGO|S_ISVTX);
  2333. error = security_inode_mkdir(dir, dentry, mode);
  2334. if (error)
  2335. return error;
  2336. if (max_links && dir->i_nlink >= max_links)
  2337. return -EMLINK;
  2338. error = dir->i_op->mkdir(dir, dentry, mode);
  2339. if (!error)
  2340. fsnotify_mkdir(dir, dentry);
  2341. return error;
  2342. }
  2343. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
  2344. {
  2345. struct dentry *dentry;
  2346. struct path path;
  2347. int error;
  2348. dentry = user_path_create(dfd, pathname, &path, 1);
  2349. if (IS_ERR(dentry))
  2350. return PTR_ERR(dentry);
  2351. if (!IS_POSIXACL(path.dentry->d_inode))
  2352. mode &= ~current_umask();
  2353. error = mnt_want_write(path.mnt);
  2354. if (error)
  2355. goto out_dput;
  2356. error = security_path_mkdir(&path, dentry, mode);
  2357. if (error)
  2358. goto out_drop_write;
  2359. error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
  2360. out_drop_write:
  2361. mnt_drop_write(path.mnt);
  2362. out_dput:
  2363. dput(dentry);
  2364. mutex_unlock(&path.dentry->d_inode->i_mutex);
  2365. path_put(&path);
  2366. return error;
  2367. }
  2368. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
  2369. {
  2370. return sys_mkdirat(AT_FDCWD, pathname, mode);
  2371. }
  2372. /*
  2373. * The dentry_unhash() helper will try to drop the dentry early: we
  2374. * should have a usage count of 1 if we're the only user of this
  2375. * dentry, and if that is true (possibly after pruning the dcache),
  2376. * then we drop the dentry now.
  2377. *
  2378. * A low-level filesystem can, if it choses, legally
  2379. * do a
  2380. *
  2381. * if (!d_unhashed(dentry))
  2382. * return -EBUSY;
  2383. *
  2384. * if it cannot handle the case of removing a directory
  2385. * that is still in use by something else..
  2386. */
  2387. void dentry_unhash(struct dentry *dentry)
  2388. {
  2389. shrink_dcache_parent(dentry);
  2390. spin_lock(&dentry->d_lock);
  2391. if (dentry->d_count == 1)
  2392. __d_drop(dentry);
  2393. spin_unlock(&dentry->d_lock);
  2394. }
  2395. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  2396. {
  2397. int error = may_delete(dir, dentry, 1);
  2398. if (error)
  2399. return error;
  2400. if (!dir->i_op->rmdir)
  2401. return -EPERM;
  2402. dget(dentry);
  2403. mutex_lock(&dentry->d_inode->i_mutex);
  2404. error = -EBUSY;
  2405. if (d_mountpoint(dentry))
  2406. goto out;
  2407. error = security_inode_rmdir(dir, dentry);
  2408. if (error)
  2409. goto out;
  2410. shrink_dcache_parent(dentry);
  2411. error = dir->i_op->rmdir(dir, dentry);
  2412. if (error)
  2413. goto out;
  2414. dentry->d_inode->i_flags |= S_DEAD;
  2415. dont_mount(dentry);
  2416. out:
  2417. mutex_unlock(&dentry->d_inode->i_mutex);
  2418. dput(dentry);
  2419. if (!error)
  2420. d_delete(dentry);
  2421. return error;
  2422. }
  2423. static long do_rmdir(int dfd, const char __user *pathname)
  2424. {
  2425. int error = 0;
  2426. char * name;
  2427. struct dentry *dentry;
  2428. struct nameidata nd;
  2429. error = user_path_parent(dfd, pathname, &nd, &name);
  2430. if (error)
  2431. return error;
  2432. switch(nd.last_type) {
  2433. case LAST_DOTDOT:
  2434. error = -ENOTEMPTY;
  2435. goto exit1;
  2436. case LAST_DOT:
  2437. error = -EINVAL;
  2438. goto exit1;
  2439. case LAST_ROOT:
  2440. error = -EBUSY;
  2441. goto exit1;
  2442. }
  2443. nd.flags &= ~LOOKUP_PARENT;
  2444. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2445. dentry = lookup_hash(&nd);
  2446. error = PTR_ERR(dentry);
  2447. if (IS_ERR(dentry))
  2448. goto exit2;
  2449. if (!dentry->d_inode) {
  2450. error = -ENOENT;
  2451. goto exit3;
  2452. }
  2453. error = mnt_want_write(nd.path.mnt);
  2454. if (error)
  2455. goto exit3;
  2456. error = security_path_rmdir(&nd.path, dentry);
  2457. if (error)
  2458. goto exit4;
  2459. error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
  2460. exit4:
  2461. mnt_drop_write(nd.path.mnt);
  2462. exit3:
  2463. dput(dentry);
  2464. exit2:
  2465. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2466. exit1:
  2467. path_put(&nd.path);
  2468. putname(name);
  2469. return error;
  2470. }
  2471. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  2472. {
  2473. return do_rmdir(AT_FDCWD, pathname);
  2474. }
  2475. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  2476. {
  2477. int error = may_delete(dir, dentry, 0);
  2478. if (error)
  2479. return error;
  2480. if (!dir->i_op->unlink)
  2481. return -EPERM;
  2482. mutex_lock(&dentry->d_inode->i_mutex);
  2483. if (d_mountpoint(dentry))
  2484. error = -EBUSY;
  2485. else {
  2486. error = security_inode_unlink(dir, dentry);
  2487. if (!error) {
  2488. error = dir->i_op->unlink(dir, dentry);
  2489. if (!error)
  2490. dont_mount(dentry);
  2491. }
  2492. }
  2493. mutex_unlock(&dentry->d_inode->i_mutex);
  2494. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  2495. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  2496. fsnotify_link_count(dentry->d_inode);
  2497. d_delete(dentry);
  2498. }
  2499. return error;
  2500. }
  2501. /*
  2502. * Make sure that the actual truncation of the file will occur outside its
  2503. * directory's i_mutex. Truncate can take a long time if there is a lot of
  2504. * writeout happening, and we don't want to prevent access to the directory
  2505. * while waiting on the I/O.
  2506. */
  2507. static long do_unlinkat(int dfd, const char __user *pathname)
  2508. {
  2509. int error;
  2510. char *name;
  2511. struct dentry *dentry;
  2512. struct nameidata nd;
  2513. struct inode *inode = NULL;
  2514. error = user_path_parent(dfd, pathname, &nd, &name);
  2515. if (error)
  2516. return error;
  2517. error = -EISDIR;
  2518. if (nd.last_type != LAST_NORM)
  2519. goto exit1;
  2520. nd.flags &= ~LOOKUP_PARENT;
  2521. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2522. dentry = lookup_hash(&nd);
  2523. error = PTR_ERR(dentry);
  2524. if (!IS_ERR(dentry)) {
  2525. /* Why not before? Because we want correct error value */
  2526. if (nd.last.name[nd.last.len])
  2527. goto slashes;
  2528. inode = dentry->d_inode;
  2529. if (!inode)
  2530. goto slashes;
  2531. ihold(inode);
  2532. error = mnt_want_write(nd.path.mnt);
  2533. if (error)
  2534. goto exit2;
  2535. error = security_path_unlink(&nd.path, dentry);
  2536. if (error)
  2537. goto exit3;
  2538. error = vfs_unlink(nd.path.dentry->d_inode, dentry);
  2539. exit3:
  2540. mnt_drop_write(nd.path.mnt);
  2541. exit2:
  2542. dput(dentry);
  2543. }
  2544. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2545. if (inode)
  2546. iput(inode); /* truncate the inode here */
  2547. exit1:
  2548. path_put(&nd.path);
  2549. putname(name);
  2550. return error;
  2551. slashes:
  2552. error = !dentry->d_inode ? -ENOENT :
  2553. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  2554. goto exit2;
  2555. }
  2556. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  2557. {
  2558. if ((flag & ~AT_REMOVEDIR) != 0)
  2559. return -EINVAL;
  2560. if (flag & AT_REMOVEDIR)
  2561. return do_rmdir(dfd, pathname);
  2562. return do_unlinkat(dfd, pathname);
  2563. }
  2564. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  2565. {
  2566. return do_unlinkat(AT_FDCWD, pathname);
  2567. }
  2568. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  2569. {
  2570. int error = may_create(dir, dentry);
  2571. if (error)
  2572. return error;
  2573. if (!dir->i_op->symlink)
  2574. return -EPERM;
  2575. error = security_inode_symlink(dir, dentry, oldname);
  2576. if (error)
  2577. return error;
  2578. error = dir->i_op->symlink(dir, dentry, oldname);
  2579. if (!error)
  2580. fsnotify_create(dir, dentry);
  2581. return error;
  2582. }
  2583. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  2584. int, newdfd, const char __user *, newname)
  2585. {
  2586. int error;
  2587. char *from;
  2588. struct dentry *dentry;
  2589. struct path path;
  2590. from = getname(oldname);
  2591. if (IS_ERR(from))
  2592. return PTR_ERR(from);
  2593. dentry = user_path_create(newdfd, newname, &path, 0);
  2594. error = PTR_ERR(dentry);
  2595. if (IS_ERR(dentry))
  2596. goto out_putname;
  2597. error = mnt_want_write(path.mnt);
  2598. if (error)
  2599. goto out_dput;
  2600. error = security_path_symlink(&path, dentry, from);
  2601. if (error)
  2602. goto out_drop_write;
  2603. error = vfs_symlink(path.dentry->d_inode, dentry, from);
  2604. out_drop_write:
  2605. mnt_drop_write(path.mnt);
  2606. out_dput:
  2607. dput(dentry);
  2608. mutex_unlock(&path.dentry->d_inode->i_mutex);
  2609. path_put(&path);
  2610. out_putname:
  2611. putname(from);
  2612. return error;
  2613. }
  2614. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  2615. {
  2616. return sys_symlinkat(oldname, AT_FDCWD, newname);
  2617. }
  2618. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2619. {
  2620. struct inode *inode = old_dentry->d_inode;
  2621. unsigned max_links = dir->i_sb->s_max_links;
  2622. int error;
  2623. if (!inode)
  2624. return -ENOENT;
  2625. error = may_create(dir, new_dentry);
  2626. if (error)
  2627. return error;
  2628. if (dir->i_sb != inode->i_sb)
  2629. return -EXDEV;
  2630. /*
  2631. * A link to an append-only or immutable file cannot be created.
  2632. */
  2633. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2634. return -EPERM;
  2635. if (!dir->i_op->link)
  2636. return -EPERM;
  2637. if (S_ISDIR(inode->i_mode))
  2638. return -EPERM;
  2639. error = security_inode_link(old_dentry, dir, new_dentry);
  2640. if (error)
  2641. return error;
  2642. mutex_lock(&inode->i_mutex);
  2643. /* Make sure we don't allow creating hardlink to an unlinked file */
  2644. if (inode->i_nlink == 0)
  2645. error = -ENOENT;
  2646. else if (max_links && inode->i_nlink >= max_links)
  2647. error = -EMLINK;
  2648. else
  2649. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2650. mutex_unlock(&inode->i_mutex);
  2651. if (!error)
  2652. fsnotify_link(dir, inode, new_dentry);
  2653. return error;
  2654. }
  2655. /*
  2656. * Hardlinks are often used in delicate situations. We avoid
  2657. * security-related surprises by not following symlinks on the
  2658. * newname. --KAB
  2659. *
  2660. * We don't follow them on the oldname either to be compatible
  2661. * with linux 2.0, and to avoid hard-linking to directories
  2662. * and other special files. --ADM
  2663. */
  2664. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  2665. int, newdfd, const char __user *, newname, int, flags)
  2666. {
  2667. struct dentry *new_dentry;
  2668. struct path old_path, new_path;
  2669. int how = 0;
  2670. int error;
  2671. if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  2672. return -EINVAL;
  2673. /*
  2674. * To use null names we require CAP_DAC_READ_SEARCH
  2675. * This ensures that not everyone will be able to create
  2676. * handlink using the passed filedescriptor.
  2677. */
  2678. if (flags & AT_EMPTY_PATH) {
  2679. if (!capable(CAP_DAC_READ_SEARCH))
  2680. return -ENOENT;
  2681. how = LOOKUP_EMPTY;
  2682. }
  2683. if (flags & AT_SYMLINK_FOLLOW)
  2684. how |= LOOKUP_FOLLOW;
  2685. error = user_path_at(olddfd, oldname, how, &old_path);
  2686. if (error)
  2687. return error;
  2688. new_dentry = user_path_create(newdfd, newname, &new_path, 0);
  2689. error = PTR_ERR(new_dentry);
  2690. if (IS_ERR(new_dentry))
  2691. goto out;
  2692. error = -EXDEV;
  2693. if (old_path.mnt != new_path.mnt)
  2694. goto out_dput;
  2695. error = mnt_want_write(new_path.mnt);
  2696. if (error)
  2697. goto out_dput;
  2698. error = security_path_link(old_path.dentry, &new_path, new_dentry);
  2699. if (error)
  2700. goto out_drop_write;
  2701. error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
  2702. out_drop_write:
  2703. mnt_drop_write(new_path.mnt);
  2704. out_dput:
  2705. dput(new_dentry);
  2706. mutex_unlock(&new_path.dentry->d_inode->i_mutex);
  2707. path_put(&new_path);
  2708. out:
  2709. path_put(&old_path);
  2710. return error;
  2711. }
  2712. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  2713. {
  2714. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2715. }
  2716. /*
  2717. * The worst of all namespace operations - renaming directory. "Perverted"
  2718. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2719. * Problems:
  2720. * a) we can get into loop creation. Check is done in is_subdir().
  2721. * b) race potential - two innocent renames can create a loop together.
  2722. * That's where 4.4 screws up. Current fix: serialization on
  2723. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2724. * story.
  2725. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2726. * And that - after we got ->i_mutex on parents (until then we don't know
  2727. * whether the target exists). Solution: try to be smart with locking
  2728. * order for inodes. We rely on the fact that tree topology may change
  2729. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2730. * move will be locked. Thus we can rank directories by the tree
  2731. * (ancestors first) and rank all non-directories after them.
  2732. * That works since everybody except rename does "lock parent, lookup,
  2733. * lock child" and rename is under ->s_vfs_rename_mutex.
  2734. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2735. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2736. * we'd better make sure that there's no link(2) for them.
  2737. * d) conversion from fhandle to dentry may come in the wrong moment - when
  2738. * we are removing the target. Solution: we will have to grab ->i_mutex
  2739. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2740. * ->i_mutex on parents, which works but leads to some truly excessive
  2741. * locking].
  2742. */
  2743. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2744. struct inode *new_dir, struct dentry *new_dentry)
  2745. {
  2746. int error = 0;
  2747. struct inode *target = new_dentry->d_inode;
  2748. unsigned max_links = new_dir->i_sb->s_max_links;
  2749. /*
  2750. * If we are going to change the parent - check write permissions,
  2751. * we'll need to flip '..'.
  2752. */
  2753. if (new_dir != old_dir) {
  2754. error = inode_permission(old_dentry->d_inode, MAY_WRITE);
  2755. if (error)
  2756. return error;
  2757. }
  2758. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2759. if (error)
  2760. return error;
  2761. dget(new_dentry);
  2762. if (target)
  2763. mutex_lock(&target->i_mutex);
  2764. error = -EBUSY;
  2765. if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
  2766. goto out;
  2767. error = -EMLINK;
  2768. if (max_links && !target && new_dir != old_dir &&
  2769. new_dir->i_nlink >= max_links)
  2770. goto out;
  2771. if (target)
  2772. shrink_dcache_parent(new_dentry);
  2773. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2774. if (error)
  2775. goto out;
  2776. if (target) {
  2777. target->i_flags |= S_DEAD;
  2778. dont_mount(new_dentry);
  2779. }
  2780. out:
  2781. if (target)
  2782. mutex_unlock(&target->i_mutex);
  2783. dput(new_dentry);
  2784. if (!error)
  2785. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2786. d_move(old_dentry,new_dentry);
  2787. return error;
  2788. }
  2789. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2790. struct inode *new_dir, struct dentry *new_dentry)
  2791. {
  2792. struct inode *target = new_dentry->d_inode;
  2793. int error;
  2794. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2795. if (error)
  2796. return error;
  2797. dget(new_dentry);
  2798. if (target)
  2799. mutex_lock(&target->i_mutex);
  2800. error = -EBUSY;
  2801. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2802. goto out;
  2803. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2804. if (error)
  2805. goto out;
  2806. if (target)
  2807. dont_mount(new_dentry);
  2808. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2809. d_move(old_dentry, new_dentry);
  2810. out:
  2811. if (target)
  2812. mutex_unlock(&target->i_mutex);
  2813. dput(new_dentry);
  2814. return error;
  2815. }
  2816. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2817. struct inode *new_dir, struct dentry *new_dentry)
  2818. {
  2819. int error;
  2820. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2821. const unsigned char *old_name;
  2822. if (old_dentry->d_inode == new_dentry->d_inode)
  2823. return 0;
  2824. error = may_delete(old_dir, old_dentry, is_dir);
  2825. if (error)
  2826. return error;
  2827. if (!new_dentry->d_inode)
  2828. error = may_create(new_dir, new_dentry);
  2829. else
  2830. error = may_delete(new_dir, new_dentry, is_dir);
  2831. if (error)
  2832. return error;
  2833. if (!old_dir->i_op->rename)
  2834. return -EPERM;
  2835. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2836. if (is_dir)
  2837. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2838. else
  2839. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2840. if (!error)
  2841. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  2842. new_dentry->d_inode, old_dentry);
  2843. fsnotify_oldname_free(old_name);
  2844. return error;
  2845. }
  2846. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  2847. int, newdfd, const char __user *, newname)
  2848. {
  2849. struct dentry *old_dir, *new_dir;
  2850. struct dentry *old_dentry, *new_dentry;
  2851. struct dentry *trap;
  2852. struct nameidata oldnd, newnd;
  2853. char *from;
  2854. char *to;
  2855. int error;
  2856. error = user_path_parent(olddfd, oldname, &oldnd, &from);
  2857. if (error)
  2858. goto exit;
  2859. error = user_path_parent(newdfd, newname, &newnd, &to);
  2860. if (error)
  2861. goto exit1;
  2862. error = -EXDEV;
  2863. if (oldnd.path.mnt != newnd.path.mnt)
  2864. goto exit2;
  2865. old_dir = oldnd.path.dentry;
  2866. error = -EBUSY;
  2867. if (oldnd.last_type != LAST_NORM)
  2868. goto exit2;
  2869. new_dir = newnd.path.dentry;
  2870. if (newnd.last_type != LAST_NORM)
  2871. goto exit2;
  2872. oldnd.flags &= ~LOOKUP_PARENT;
  2873. newnd.flags &= ~LOOKUP_PARENT;
  2874. newnd.flags |= LOOKUP_RENAME_TARGET;
  2875. trap = lock_rename(new_dir, old_dir);
  2876. old_dentry = lookup_hash(&oldnd);
  2877. error = PTR_ERR(old_dentry);
  2878. if (IS_ERR(old_dentry))
  2879. goto exit3;
  2880. /* source must exist */
  2881. error = -ENOENT;
  2882. if (!old_dentry->d_inode)
  2883. goto exit4;
  2884. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2885. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2886. error = -ENOTDIR;
  2887. if (oldnd.last.name[oldnd.last.len])
  2888. goto exit4;
  2889. if (newnd.last.name[newnd.last.len])
  2890. goto exit4;
  2891. }
  2892. /* source should not be ancestor of target */
  2893. error = -EINVAL;
  2894. if (old_dentry == trap)
  2895. goto exit4;
  2896. new_dentry = lookup_hash(&newnd);
  2897. error = PTR_ERR(new_dentry);
  2898. if (IS_ERR(new_dentry))
  2899. goto exit4;
  2900. /* target should not be an ancestor of source */
  2901. error = -ENOTEMPTY;
  2902. if (new_dentry == trap)
  2903. goto exit5;
  2904. error = mnt_want_write(oldnd.path.mnt);
  2905. if (error)
  2906. goto exit5;
  2907. error = security_path_rename(&oldnd.path, old_dentry,
  2908. &newnd.path, new_dentry);
  2909. if (error)
  2910. goto exit6;
  2911. error = vfs_rename(old_dir->d_inode, old_dentry,
  2912. new_dir->d_inode, new_dentry);
  2913. exit6:
  2914. mnt_drop_write(oldnd.path.mnt);
  2915. exit5:
  2916. dput(new_dentry);
  2917. exit4:
  2918. dput(old_dentry);
  2919. exit3:
  2920. unlock_rename(new_dir, old_dir);
  2921. exit2:
  2922. path_put(&newnd.path);
  2923. putname(to);
  2924. exit1:
  2925. path_put(&oldnd.path);
  2926. putname(from);
  2927. exit:
  2928. return error;
  2929. }
  2930. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  2931. {
  2932. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2933. }
  2934. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2935. {
  2936. int len;
  2937. len = PTR_ERR(link);
  2938. if (IS_ERR(link))
  2939. goto out;
  2940. len = strlen(link);
  2941. if (len > (unsigned) buflen)
  2942. len = buflen;
  2943. if (copy_to_user(buffer, link, len))
  2944. len = -EFAULT;
  2945. out:
  2946. return len;
  2947. }
  2948. /*
  2949. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2950. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2951. * using) it for any given inode is up to filesystem.
  2952. */
  2953. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2954. {
  2955. struct nameidata nd;
  2956. void *cookie;
  2957. int res;
  2958. nd.depth = 0;
  2959. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2960. if (IS_ERR(cookie))
  2961. return PTR_ERR(cookie);
  2962. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2963. if (dentry->d_inode->i_op->put_link)
  2964. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2965. return res;
  2966. }
  2967. int vfs_follow_link(struct nameidata *nd, const char *link)
  2968. {
  2969. return __vfs_follow_link(nd, link);
  2970. }
  2971. /* get the link contents into pagecache */
  2972. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2973. {
  2974. char *kaddr;
  2975. struct page *page;
  2976. struct address_space *mapping = dentry->d_inode->i_mapping;
  2977. page = read_mapping_page(mapping, 0, NULL);
  2978. if (IS_ERR(page))
  2979. return (char*)page;
  2980. *ppage = page;
  2981. kaddr = kmap(page);
  2982. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  2983. return kaddr;
  2984. }
  2985. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2986. {
  2987. struct page *page = NULL;
  2988. char *s = page_getlink(dentry, &page);
  2989. int res = vfs_readlink(dentry,buffer,buflen,s);
  2990. if (page) {
  2991. kunmap(page);
  2992. page_cache_release(page);
  2993. }
  2994. return res;
  2995. }
  2996. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2997. {
  2998. struct page *page = NULL;
  2999. nd_set_link(nd, page_getlink(dentry, &page));
  3000. return page;
  3001. }
  3002. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  3003. {
  3004. struct page *page = cookie;
  3005. if (page) {
  3006. kunmap(page);
  3007. page_cache_release(page);
  3008. }
  3009. }
  3010. /*
  3011. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  3012. */
  3013. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  3014. {
  3015. struct address_space *mapping = inode->i_mapping;
  3016. struct page *page;
  3017. void *fsdata;
  3018. int err;
  3019. char *kaddr;
  3020. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  3021. if (nofs)
  3022. flags |= AOP_FLAG_NOFS;
  3023. retry:
  3024. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  3025. flags, &page, &fsdata);
  3026. if (err)
  3027. goto fail;
  3028. kaddr = kmap_atomic(page);
  3029. memcpy(kaddr, symname, len-1);
  3030. kunmap_atomic(kaddr);
  3031. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  3032. page, fsdata);
  3033. if (err < 0)
  3034. goto fail;
  3035. if (err < len-1)
  3036. goto retry;
  3037. mark_inode_dirty(inode);
  3038. return 0;
  3039. fail:
  3040. return err;
  3041. }
  3042. int page_symlink(struct inode *inode, const char *symname, int len)
  3043. {
  3044. return __page_symlink(inode, symname, len,
  3045. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  3046. }
  3047. const struct inode_operations page_symlink_inode_operations = {
  3048. .readlink = generic_readlink,
  3049. .follow_link = page_follow_link_light,
  3050. .put_link = page_put_link,
  3051. };
  3052. EXPORT_SYMBOL(user_path_at);
  3053. EXPORT_SYMBOL(follow_down_one);
  3054. EXPORT_SYMBOL(follow_down);
  3055. EXPORT_SYMBOL(follow_up);
  3056. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  3057. EXPORT_SYMBOL(getname);
  3058. EXPORT_SYMBOL(lock_rename);
  3059. EXPORT_SYMBOL(lookup_one_len);
  3060. EXPORT_SYMBOL(page_follow_link_light);
  3061. EXPORT_SYMBOL(page_put_link);
  3062. EXPORT_SYMBOL(page_readlink);
  3063. EXPORT_SYMBOL(__page_symlink);
  3064. EXPORT_SYMBOL(page_symlink);
  3065. EXPORT_SYMBOL(page_symlink_inode_operations);
  3066. EXPORT_SYMBOL(kern_path);
  3067. EXPORT_SYMBOL(vfs_path_lookup);
  3068. EXPORT_SYMBOL(inode_permission);
  3069. EXPORT_SYMBOL(unlock_rename);
  3070. EXPORT_SYMBOL(vfs_create);
  3071. EXPORT_SYMBOL(vfs_follow_link);
  3072. EXPORT_SYMBOL(vfs_link);
  3073. EXPORT_SYMBOL(vfs_mkdir);
  3074. EXPORT_SYMBOL(vfs_mknod);
  3075. EXPORT_SYMBOL(generic_permission);
  3076. EXPORT_SYMBOL(vfs_readlink);
  3077. EXPORT_SYMBOL(vfs_rename);
  3078. EXPORT_SYMBOL(vfs_rmdir);
  3079. EXPORT_SYMBOL(vfs_symlink);
  3080. EXPORT_SYMBOL(vfs_unlink);
  3081. EXPORT_SYMBOL(dentry_unhash);
  3082. EXPORT_SYMBOL(generic_readlink);