namei.c 81 KB

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