namei.c 85 KB

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