namei.c 81 KB

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