namei.c 82 KB

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