namei.c 83 KB

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