namei.c 82 KB

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