namei.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352
  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. if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
  840. return false;
  841. return true;
  842. }
  843. static void follow_mount_rcu(struct nameidata *nd)
  844. {
  845. while (d_mountpoint(nd->path.dentry)) {
  846. struct vfsmount *mounted;
  847. mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
  848. if (!mounted)
  849. break;
  850. nd->path.mnt = mounted;
  851. nd->path.dentry = mounted->mnt_root;
  852. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  853. }
  854. }
  855. static int follow_dotdot_rcu(struct nameidata *nd)
  856. {
  857. set_root_rcu(nd);
  858. while (1) {
  859. if (nd->path.dentry == nd->root.dentry &&
  860. nd->path.mnt == nd->root.mnt) {
  861. break;
  862. }
  863. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  864. struct dentry *old = nd->path.dentry;
  865. struct dentry *parent = old->d_parent;
  866. unsigned seq;
  867. seq = read_seqcount_begin(&parent->d_seq);
  868. if (read_seqcount_retry(&old->d_seq, nd->seq))
  869. goto failed;
  870. nd->path.dentry = parent;
  871. nd->seq = seq;
  872. break;
  873. }
  874. if (!follow_up_rcu(&nd->path))
  875. break;
  876. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  877. }
  878. follow_mount_rcu(nd);
  879. nd->inode = nd->path.dentry->d_inode;
  880. return 0;
  881. failed:
  882. nd->flags &= ~LOOKUP_RCU;
  883. if (!(nd->flags & LOOKUP_ROOT))
  884. nd->root.mnt = NULL;
  885. rcu_read_unlock();
  886. br_read_unlock(vfsmount_lock);
  887. return -ECHILD;
  888. }
  889. /*
  890. * Follow down to the covering mount currently visible to userspace. At each
  891. * point, the filesystem owning that dentry may be queried as to whether the
  892. * caller is permitted to proceed or not.
  893. *
  894. * Care must be taken as namespace_sem may be held (indicated by mounting_here
  895. * being true).
  896. */
  897. int follow_down(struct path *path)
  898. {
  899. unsigned managed;
  900. int ret;
  901. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  902. unlikely(managed & DCACHE_MANAGED_DENTRY)) {
  903. /* Allow the filesystem to manage the transit without i_mutex
  904. * being held.
  905. *
  906. * We indicate to the filesystem if someone is trying to mount
  907. * something here. This gives autofs the chance to deny anyone
  908. * other than its daemon the right to mount on its
  909. * superstructure.
  910. *
  911. * The filesystem may sleep at this point.
  912. */
  913. if (managed & DCACHE_MANAGE_TRANSIT) {
  914. BUG_ON(!path->dentry->d_op);
  915. BUG_ON(!path->dentry->d_op->d_manage);
  916. ret = path->dentry->d_op->d_manage(
  917. path->dentry, false);
  918. if (ret < 0)
  919. return ret == -EISDIR ? 0 : ret;
  920. }
  921. /* Transit to a mounted filesystem. */
  922. if (managed & DCACHE_MOUNTED) {
  923. struct vfsmount *mounted = lookup_mnt(path);
  924. if (!mounted)
  925. break;
  926. dput(path->dentry);
  927. mntput(path->mnt);
  928. path->mnt = mounted;
  929. path->dentry = dget(mounted->mnt_root);
  930. continue;
  931. }
  932. /* Don't handle automount points here */
  933. break;
  934. }
  935. return 0;
  936. }
  937. /*
  938. * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
  939. */
  940. static void follow_mount(struct path *path)
  941. {
  942. while (d_mountpoint(path->dentry)) {
  943. struct vfsmount *mounted = lookup_mnt(path);
  944. if (!mounted)
  945. break;
  946. dput(path->dentry);
  947. mntput(path->mnt);
  948. path->mnt = mounted;
  949. path->dentry = dget(mounted->mnt_root);
  950. }
  951. }
  952. static void follow_dotdot(struct nameidata *nd)
  953. {
  954. set_root(nd);
  955. while(1) {
  956. struct dentry *old = nd->path.dentry;
  957. if (nd->path.dentry == nd->root.dentry &&
  958. nd->path.mnt == nd->root.mnt) {
  959. break;
  960. }
  961. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  962. /* rare case of legitimate dget_parent()... */
  963. nd->path.dentry = dget_parent(nd->path.dentry);
  964. dput(old);
  965. break;
  966. }
  967. if (!follow_up(&nd->path))
  968. break;
  969. }
  970. follow_mount(&nd->path);
  971. nd->inode = nd->path.dentry->d_inode;
  972. }
  973. /*
  974. * Allocate a dentry with name and parent, and perform a parent
  975. * directory ->lookup on it. Returns the new dentry, or ERR_PTR
  976. * on error. parent->d_inode->i_mutex must be held. d_lookup must
  977. * have verified that no child exists while under i_mutex.
  978. */
  979. static struct dentry *d_alloc_and_lookup(struct dentry *parent,
  980. struct qstr *name, struct nameidata *nd)
  981. {
  982. struct inode *inode = parent->d_inode;
  983. struct dentry *dentry;
  984. struct dentry *old;
  985. /* Don't create child dentry for a dead directory. */
  986. if (unlikely(IS_DEADDIR(inode)))
  987. return ERR_PTR(-ENOENT);
  988. dentry = d_alloc(parent, name);
  989. if (unlikely(!dentry))
  990. return ERR_PTR(-ENOMEM);
  991. old = inode->i_op->lookup(inode, dentry, nd);
  992. if (unlikely(old)) {
  993. dput(dentry);
  994. dentry = old;
  995. }
  996. return dentry;
  997. }
  998. /*
  999. * It's more convoluted than I'd like it to be, but... it's still fairly
  1000. * small and for now I'd prefer to have fast path as straight as possible.
  1001. * It _is_ time-critical.
  1002. */
  1003. static int do_lookup(struct nameidata *nd, struct qstr *name,
  1004. struct path *path, struct inode **inode)
  1005. {
  1006. struct vfsmount *mnt = nd->path.mnt;
  1007. struct dentry *dentry, *parent = nd->path.dentry;
  1008. int need_reval = 1;
  1009. int status = 1;
  1010. int err;
  1011. /*
  1012. * Rename seqlock is not required here because in the off chance
  1013. * of a false negative due to a concurrent rename, we're going to
  1014. * do the non-racy lookup, below.
  1015. */
  1016. if (nd->flags & LOOKUP_RCU) {
  1017. unsigned seq;
  1018. *inode = nd->inode;
  1019. dentry = __d_lookup_rcu(parent, name, &seq, inode);
  1020. if (!dentry)
  1021. goto unlazy;
  1022. /* Memory barrier in read_seqcount_begin of child is enough */
  1023. if (__read_seqcount_retry(&parent->d_seq, nd->seq))
  1024. return -ECHILD;
  1025. nd->seq = seq;
  1026. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1027. status = d_revalidate(dentry, nd);
  1028. if (unlikely(status <= 0)) {
  1029. if (status != -ECHILD)
  1030. need_reval = 0;
  1031. goto unlazy;
  1032. }
  1033. }
  1034. path->mnt = mnt;
  1035. path->dentry = dentry;
  1036. if (likely(__follow_mount_rcu(nd, path, inode)))
  1037. return 0;
  1038. unlazy:
  1039. if (unlazy_walk(nd, dentry))
  1040. return -ECHILD;
  1041. } else {
  1042. dentry = __d_lookup(parent, name);
  1043. }
  1044. retry:
  1045. if (unlikely(!dentry)) {
  1046. struct inode *dir = parent->d_inode;
  1047. BUG_ON(nd->inode != dir);
  1048. mutex_lock(&dir->i_mutex);
  1049. dentry = d_lookup(parent, name);
  1050. if (likely(!dentry)) {
  1051. dentry = d_alloc_and_lookup(parent, name, nd);
  1052. if (IS_ERR(dentry)) {
  1053. mutex_unlock(&dir->i_mutex);
  1054. return PTR_ERR(dentry);
  1055. }
  1056. /* known good */
  1057. need_reval = 0;
  1058. status = 1;
  1059. }
  1060. mutex_unlock(&dir->i_mutex);
  1061. }
  1062. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
  1063. status = d_revalidate(dentry, nd);
  1064. if (unlikely(status <= 0)) {
  1065. if (status < 0) {
  1066. dput(dentry);
  1067. return status;
  1068. }
  1069. if (!d_invalidate(dentry)) {
  1070. dput(dentry);
  1071. dentry = NULL;
  1072. need_reval = 1;
  1073. goto retry;
  1074. }
  1075. }
  1076. path->mnt = mnt;
  1077. path->dentry = dentry;
  1078. err = follow_managed(path, nd->flags);
  1079. if (unlikely(err < 0)) {
  1080. path_put_conditional(path, nd);
  1081. return err;
  1082. }
  1083. *inode = path->dentry->d_inode;
  1084. return 0;
  1085. }
  1086. static inline int may_lookup(struct nameidata *nd)
  1087. {
  1088. if (nd->flags & LOOKUP_RCU) {
  1089. int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
  1090. if (err != -ECHILD)
  1091. return err;
  1092. if (unlazy_walk(nd, NULL))
  1093. return -ECHILD;
  1094. }
  1095. return exec_permission(nd->inode, 0);
  1096. }
  1097. static inline int handle_dots(struct nameidata *nd, int type)
  1098. {
  1099. if (type == LAST_DOTDOT) {
  1100. if (nd->flags & LOOKUP_RCU) {
  1101. if (follow_dotdot_rcu(nd))
  1102. return -ECHILD;
  1103. } else
  1104. follow_dotdot(nd);
  1105. }
  1106. return 0;
  1107. }
  1108. static void terminate_walk(struct nameidata *nd)
  1109. {
  1110. if (!(nd->flags & LOOKUP_RCU)) {
  1111. path_put(&nd->path);
  1112. } else {
  1113. nd->flags &= ~LOOKUP_RCU;
  1114. if (!(nd->flags & LOOKUP_ROOT))
  1115. nd->root.mnt = NULL;
  1116. rcu_read_unlock();
  1117. br_read_unlock(vfsmount_lock);
  1118. }
  1119. }
  1120. static inline int walk_component(struct nameidata *nd, struct path *path,
  1121. struct qstr *name, int type, int follow)
  1122. {
  1123. struct inode *inode;
  1124. int err;
  1125. /*
  1126. * "." and ".." are special - ".." especially so because it has
  1127. * to be able to know about the current root directory and
  1128. * parent relationships.
  1129. */
  1130. if (unlikely(type != LAST_NORM))
  1131. return handle_dots(nd, type);
  1132. err = do_lookup(nd, name, path, &inode);
  1133. if (unlikely(err)) {
  1134. terminate_walk(nd);
  1135. return err;
  1136. }
  1137. if (!inode) {
  1138. path_to_nameidata(path, nd);
  1139. terminate_walk(nd);
  1140. return -ENOENT;
  1141. }
  1142. if (unlikely(inode->i_op->follow_link) && follow) {
  1143. if (nd->flags & LOOKUP_RCU) {
  1144. if (unlikely(unlazy_walk(nd, path->dentry))) {
  1145. terminate_walk(nd);
  1146. return -ECHILD;
  1147. }
  1148. }
  1149. BUG_ON(inode != path->dentry->d_inode);
  1150. return 1;
  1151. }
  1152. path_to_nameidata(path, nd);
  1153. nd->inode = inode;
  1154. return 0;
  1155. }
  1156. /*
  1157. * This limits recursive symlink follows to 8, while
  1158. * limiting consecutive symlinks to 40.
  1159. *
  1160. * Without that kind of total limit, nasty chains of consecutive
  1161. * symlinks can cause almost arbitrarily long lookups.
  1162. */
  1163. static inline int nested_symlink(struct path *path, struct nameidata *nd)
  1164. {
  1165. int res;
  1166. if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
  1167. path_put_conditional(path, nd);
  1168. path_put(&nd->path);
  1169. return -ELOOP;
  1170. }
  1171. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  1172. nd->depth++;
  1173. current->link_count++;
  1174. do {
  1175. struct path link = *path;
  1176. void *cookie;
  1177. res = follow_link(&link, nd, &cookie);
  1178. if (!res)
  1179. res = walk_component(nd, path, &nd->last,
  1180. nd->last_type, LOOKUP_FOLLOW);
  1181. put_link(nd, &link, cookie);
  1182. } while (res > 0);
  1183. current->link_count--;
  1184. nd->depth--;
  1185. return res;
  1186. }
  1187. /*
  1188. * Name resolution.
  1189. * This is the basic name resolution function, turning a pathname into
  1190. * the final dentry. We expect 'base' to be positive and a directory.
  1191. *
  1192. * Returns 0 and nd will have valid dentry and mnt on success.
  1193. * Returns error and drops reference to input namei data on failure.
  1194. */
  1195. static int link_path_walk(const char *name, struct nameidata *nd)
  1196. {
  1197. struct path next;
  1198. int err;
  1199. unsigned int lookup_flags = nd->flags;
  1200. while (*name=='/')
  1201. name++;
  1202. if (!*name)
  1203. return 0;
  1204. /* At this point we know we have a real path component. */
  1205. for(;;) {
  1206. unsigned long hash;
  1207. struct qstr this;
  1208. unsigned int c;
  1209. int type;
  1210. nd->flags |= LOOKUP_CONTINUE;
  1211. err = may_lookup(nd);
  1212. if (err)
  1213. break;
  1214. this.name = name;
  1215. c = *(const unsigned char *)name;
  1216. hash = init_name_hash();
  1217. do {
  1218. name++;
  1219. hash = partial_name_hash(c, hash);
  1220. c = *(const unsigned char *)name;
  1221. } while (c && (c != '/'));
  1222. this.len = name - (const char *) this.name;
  1223. this.hash = end_name_hash(hash);
  1224. type = LAST_NORM;
  1225. if (this.name[0] == '.') switch (this.len) {
  1226. case 2:
  1227. if (this.name[1] == '.') {
  1228. type = LAST_DOTDOT;
  1229. nd->flags |= LOOKUP_JUMPED;
  1230. }
  1231. break;
  1232. case 1:
  1233. type = LAST_DOT;
  1234. }
  1235. if (likely(type == LAST_NORM)) {
  1236. struct dentry *parent = nd->path.dentry;
  1237. nd->flags &= ~LOOKUP_JUMPED;
  1238. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1239. err = parent->d_op->d_hash(parent, nd->inode,
  1240. &this);
  1241. if (err < 0)
  1242. break;
  1243. }
  1244. }
  1245. /* remove trailing slashes? */
  1246. if (!c)
  1247. goto last_component;
  1248. while (*++name == '/');
  1249. if (!*name)
  1250. goto last_component;
  1251. err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
  1252. if (err < 0)
  1253. return err;
  1254. if (err) {
  1255. err = nested_symlink(&next, nd);
  1256. if (err)
  1257. return err;
  1258. }
  1259. err = -ENOTDIR;
  1260. if (!nd->inode->i_op->lookup)
  1261. break;
  1262. continue;
  1263. /* here ends the main loop */
  1264. last_component:
  1265. /* Clear LOOKUP_CONTINUE iff it was previously unset */
  1266. nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
  1267. nd->last = this;
  1268. nd->last_type = type;
  1269. return 0;
  1270. }
  1271. terminate_walk(nd);
  1272. return err;
  1273. }
  1274. static int path_init(int dfd, const char *name, unsigned int flags,
  1275. struct nameidata *nd, struct file **fp)
  1276. {
  1277. int retval = 0;
  1278. int fput_needed;
  1279. struct file *file;
  1280. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1281. nd->flags = flags | LOOKUP_JUMPED;
  1282. nd->depth = 0;
  1283. if (flags & LOOKUP_ROOT) {
  1284. struct inode *inode = nd->root.dentry->d_inode;
  1285. if (*name) {
  1286. if (!inode->i_op->lookup)
  1287. return -ENOTDIR;
  1288. retval = inode_permission(inode, MAY_EXEC);
  1289. if (retval)
  1290. return retval;
  1291. }
  1292. nd->path = nd->root;
  1293. nd->inode = inode;
  1294. if (flags & LOOKUP_RCU) {
  1295. br_read_lock(vfsmount_lock);
  1296. rcu_read_lock();
  1297. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1298. } else {
  1299. path_get(&nd->path);
  1300. }
  1301. return 0;
  1302. }
  1303. nd->root.mnt = NULL;
  1304. if (*name=='/') {
  1305. if (flags & LOOKUP_RCU) {
  1306. br_read_lock(vfsmount_lock);
  1307. rcu_read_lock();
  1308. set_root_rcu(nd);
  1309. } else {
  1310. set_root(nd);
  1311. path_get(&nd->root);
  1312. }
  1313. nd->path = nd->root;
  1314. } else if (dfd == AT_FDCWD) {
  1315. if (flags & LOOKUP_RCU) {
  1316. struct fs_struct *fs = current->fs;
  1317. unsigned seq;
  1318. br_read_lock(vfsmount_lock);
  1319. rcu_read_lock();
  1320. do {
  1321. seq = read_seqcount_begin(&fs->seq);
  1322. nd->path = fs->pwd;
  1323. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1324. } while (read_seqcount_retry(&fs->seq, seq));
  1325. } else {
  1326. get_fs_pwd(current->fs, &nd->path);
  1327. }
  1328. } else {
  1329. struct dentry *dentry;
  1330. file = fget_raw_light(dfd, &fput_needed);
  1331. retval = -EBADF;
  1332. if (!file)
  1333. goto out_fail;
  1334. dentry = file->f_path.dentry;
  1335. if (*name) {
  1336. retval = -ENOTDIR;
  1337. if (!S_ISDIR(dentry->d_inode->i_mode))
  1338. goto fput_fail;
  1339. retval = file_permission(file, MAY_EXEC);
  1340. if (retval)
  1341. goto fput_fail;
  1342. }
  1343. nd->path = file->f_path;
  1344. if (flags & LOOKUP_RCU) {
  1345. if (fput_needed)
  1346. *fp = file;
  1347. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1348. br_read_lock(vfsmount_lock);
  1349. rcu_read_lock();
  1350. } else {
  1351. path_get(&file->f_path);
  1352. fput_light(file, fput_needed);
  1353. }
  1354. }
  1355. nd->inode = nd->path.dentry->d_inode;
  1356. return 0;
  1357. fput_fail:
  1358. fput_light(file, fput_needed);
  1359. out_fail:
  1360. return retval;
  1361. }
  1362. static inline int lookup_last(struct nameidata *nd, struct path *path)
  1363. {
  1364. if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
  1365. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1366. nd->flags &= ~LOOKUP_PARENT;
  1367. return walk_component(nd, path, &nd->last, nd->last_type,
  1368. nd->flags & LOOKUP_FOLLOW);
  1369. }
  1370. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1371. static int path_lookupat(int dfd, const char *name,
  1372. unsigned int flags, struct nameidata *nd)
  1373. {
  1374. struct file *base = NULL;
  1375. struct path path;
  1376. int err;
  1377. /*
  1378. * Path walking is largely split up into 2 different synchronisation
  1379. * schemes, rcu-walk and ref-walk (explained in
  1380. * Documentation/filesystems/path-lookup.txt). These share much of the
  1381. * path walk code, but some things particularly setup, cleanup, and
  1382. * following mounts are sufficiently divergent that functions are
  1383. * duplicated. Typically there is a function foo(), and its RCU
  1384. * analogue, foo_rcu().
  1385. *
  1386. * -ECHILD is the error number of choice (just to avoid clashes) that
  1387. * is returned if some aspect of an rcu-walk fails. Such an error must
  1388. * be handled by restarting a traditional ref-walk (which will always
  1389. * be able to complete).
  1390. */
  1391. err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
  1392. if (unlikely(err))
  1393. return err;
  1394. current->total_link_count = 0;
  1395. err = link_path_walk(name, nd);
  1396. if (!err && !(flags & LOOKUP_PARENT)) {
  1397. err = lookup_last(nd, &path);
  1398. while (err > 0) {
  1399. void *cookie;
  1400. struct path link = path;
  1401. nd->flags |= LOOKUP_PARENT;
  1402. err = follow_link(&link, nd, &cookie);
  1403. if (!err)
  1404. err = lookup_last(nd, &path);
  1405. put_link(nd, &link, cookie);
  1406. }
  1407. }
  1408. if (!err)
  1409. err = complete_walk(nd);
  1410. if (!err && nd->flags & LOOKUP_DIRECTORY) {
  1411. if (!nd->inode->i_op->lookup) {
  1412. path_put(&nd->path);
  1413. err = -ENOTDIR;
  1414. }
  1415. }
  1416. if (base)
  1417. fput(base);
  1418. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  1419. path_put(&nd->root);
  1420. nd->root.mnt = NULL;
  1421. }
  1422. return err;
  1423. }
  1424. static int do_path_lookup(int dfd, const char *name,
  1425. unsigned int flags, struct nameidata *nd)
  1426. {
  1427. int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
  1428. if (unlikely(retval == -ECHILD))
  1429. retval = path_lookupat(dfd, name, flags, nd);
  1430. if (unlikely(retval == -ESTALE))
  1431. retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
  1432. if (likely(!retval)) {
  1433. if (unlikely(!audit_dummy_context())) {
  1434. if (nd->path.dentry && nd->inode)
  1435. audit_inode(name, nd->path.dentry);
  1436. }
  1437. }
  1438. return retval;
  1439. }
  1440. int kern_path_parent(const char *name, struct nameidata *nd)
  1441. {
  1442. return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
  1443. }
  1444. int kern_path(const char *name, unsigned int flags, struct path *path)
  1445. {
  1446. struct nameidata nd;
  1447. int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  1448. if (!res)
  1449. *path = nd.path;
  1450. return res;
  1451. }
  1452. /**
  1453. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  1454. * @dentry: pointer to dentry of the base directory
  1455. * @mnt: pointer to vfs mount of the base directory
  1456. * @name: pointer to file name
  1457. * @flags: lookup flags
  1458. * @nd: pointer to nameidata
  1459. */
  1460. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1461. const char *name, unsigned int flags,
  1462. struct nameidata *nd)
  1463. {
  1464. nd->root.dentry = dentry;
  1465. nd->root.mnt = mnt;
  1466. /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
  1467. return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
  1468. }
  1469. static struct dentry *__lookup_hash(struct qstr *name,
  1470. struct dentry *base, struct nameidata *nd)
  1471. {
  1472. struct inode *inode = base->d_inode;
  1473. struct dentry *dentry;
  1474. int err;
  1475. err = exec_permission(inode, 0);
  1476. if (err)
  1477. return ERR_PTR(err);
  1478. /*
  1479. * Don't bother with __d_lookup: callers are for creat as
  1480. * well as unlink, so a lot of the time it would cost
  1481. * a double lookup.
  1482. */
  1483. dentry = d_lookup(base, name);
  1484. if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
  1485. dentry = do_revalidate(dentry, nd);
  1486. if (!dentry)
  1487. dentry = d_alloc_and_lookup(base, name, nd);
  1488. return dentry;
  1489. }
  1490. /*
  1491. * Restricted form of lookup. Doesn't follow links, single-component only,
  1492. * needs parent already locked. Doesn't follow mounts.
  1493. * SMP-safe.
  1494. */
  1495. static struct dentry *lookup_hash(struct nameidata *nd)
  1496. {
  1497. return __lookup_hash(&nd->last, nd->path.dentry, nd);
  1498. }
  1499. /**
  1500. * lookup_one_len - filesystem helper to lookup single pathname component
  1501. * @name: pathname component to lookup
  1502. * @base: base directory to lookup from
  1503. * @len: maximum length @len should be interpreted to
  1504. *
  1505. * Note that this routine is purely a helper for filesystem usage and should
  1506. * not be called by generic code. Also note that by using this function the
  1507. * nameidata argument is passed to the filesystem methods and a filesystem
  1508. * using this helper needs to be prepared for that.
  1509. */
  1510. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1511. {
  1512. struct qstr this;
  1513. unsigned long hash;
  1514. unsigned int c;
  1515. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  1516. this.name = name;
  1517. this.len = len;
  1518. if (!len)
  1519. return ERR_PTR(-EACCES);
  1520. hash = init_name_hash();
  1521. while (len--) {
  1522. c = *(const unsigned char *)name++;
  1523. if (c == '/' || c == '\0')
  1524. return ERR_PTR(-EACCES);
  1525. hash = partial_name_hash(c, hash);
  1526. }
  1527. this.hash = end_name_hash(hash);
  1528. /*
  1529. * See if the low-level filesystem might want
  1530. * to use its own hash..
  1531. */
  1532. if (base->d_flags & DCACHE_OP_HASH) {
  1533. int err = base->d_op->d_hash(base, base->d_inode, &this);
  1534. if (err < 0)
  1535. return ERR_PTR(err);
  1536. }
  1537. return __lookup_hash(&this, base, NULL);
  1538. }
  1539. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1540. struct path *path)
  1541. {
  1542. struct nameidata nd;
  1543. char *tmp = getname_flags(name, flags);
  1544. int err = PTR_ERR(tmp);
  1545. if (!IS_ERR(tmp)) {
  1546. BUG_ON(flags & LOOKUP_PARENT);
  1547. err = do_path_lookup(dfd, tmp, flags, &nd);
  1548. putname(tmp);
  1549. if (!err)
  1550. *path = nd.path;
  1551. }
  1552. return err;
  1553. }
  1554. static int user_path_parent(int dfd, const char __user *path,
  1555. struct nameidata *nd, char **name)
  1556. {
  1557. char *s = getname(path);
  1558. int error;
  1559. if (IS_ERR(s))
  1560. return PTR_ERR(s);
  1561. error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
  1562. if (error)
  1563. putname(s);
  1564. else
  1565. *name = s;
  1566. return error;
  1567. }
  1568. /*
  1569. * It's inline, so penalty for filesystems that don't use sticky bit is
  1570. * minimal.
  1571. */
  1572. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1573. {
  1574. uid_t fsuid = current_fsuid();
  1575. if (!(dir->i_mode & S_ISVTX))
  1576. return 0;
  1577. if (current_user_ns() != inode_userns(inode))
  1578. goto other_userns;
  1579. if (inode->i_uid == fsuid)
  1580. return 0;
  1581. if (dir->i_uid == fsuid)
  1582. return 0;
  1583. other_userns:
  1584. return !ns_capable(inode_userns(inode), CAP_FOWNER);
  1585. }
  1586. /*
  1587. * Check whether we can remove a link victim from directory dir, check
  1588. * whether the type of victim is right.
  1589. * 1. We can't do it if dir is read-only (done in permission())
  1590. * 2. We should have write and exec permissions on dir
  1591. * 3. We can't remove anything from append-only dir
  1592. * 4. We can't do anything with immutable dir (done in permission())
  1593. * 5. If the sticky bit on dir is set we should either
  1594. * a. be owner of dir, or
  1595. * b. be owner of victim, or
  1596. * c. have CAP_FOWNER capability
  1597. * 6. If the victim is append-only or immutable we can't do antyhing with
  1598. * links pointing to it.
  1599. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1600. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1601. * 9. We can't remove a root or mountpoint.
  1602. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1603. * nfs_async_unlink().
  1604. */
  1605. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1606. {
  1607. int error;
  1608. if (!victim->d_inode)
  1609. return -ENOENT;
  1610. BUG_ON(victim->d_parent->d_inode != dir);
  1611. audit_inode_child(victim, dir);
  1612. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1613. if (error)
  1614. return error;
  1615. if (IS_APPEND(dir))
  1616. return -EPERM;
  1617. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1618. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  1619. return -EPERM;
  1620. if (isdir) {
  1621. if (!S_ISDIR(victim->d_inode->i_mode))
  1622. return -ENOTDIR;
  1623. if (IS_ROOT(victim))
  1624. return -EBUSY;
  1625. } else if (S_ISDIR(victim->d_inode->i_mode))
  1626. return -EISDIR;
  1627. if (IS_DEADDIR(dir))
  1628. return -ENOENT;
  1629. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1630. return -EBUSY;
  1631. return 0;
  1632. }
  1633. /* Check whether we can create an object with dentry child in directory
  1634. * dir.
  1635. * 1. We can't do it if child already exists (open has special treatment for
  1636. * this case, but since we are inlined it's OK)
  1637. * 2. We can't do it if dir is read-only (done in permission())
  1638. * 3. We should have write and exec permissions on dir
  1639. * 4. We can't do it if dir is immutable (done in permission())
  1640. */
  1641. static inline int may_create(struct inode *dir, struct dentry *child)
  1642. {
  1643. if (child->d_inode)
  1644. return -EEXIST;
  1645. if (IS_DEADDIR(dir))
  1646. return -ENOENT;
  1647. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1648. }
  1649. /*
  1650. * p1 and p2 should be directories on the same fs.
  1651. */
  1652. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1653. {
  1654. struct dentry *p;
  1655. if (p1 == p2) {
  1656. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1657. return NULL;
  1658. }
  1659. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1660. p = d_ancestor(p2, p1);
  1661. if (p) {
  1662. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1663. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1664. return p;
  1665. }
  1666. p = d_ancestor(p1, p2);
  1667. if (p) {
  1668. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1669. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1670. return p;
  1671. }
  1672. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1673. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1674. return NULL;
  1675. }
  1676. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1677. {
  1678. mutex_unlock(&p1->d_inode->i_mutex);
  1679. if (p1 != p2) {
  1680. mutex_unlock(&p2->d_inode->i_mutex);
  1681. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1682. }
  1683. }
  1684. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1685. struct nameidata *nd)
  1686. {
  1687. int error = may_create(dir, dentry);
  1688. if (error)
  1689. return error;
  1690. if (!dir->i_op->create)
  1691. return -EACCES; /* shouldn't it be ENOSYS? */
  1692. mode &= S_IALLUGO;
  1693. mode |= S_IFREG;
  1694. error = security_inode_create(dir, dentry, mode);
  1695. if (error)
  1696. return error;
  1697. error = dir->i_op->create(dir, dentry, mode, nd);
  1698. if (!error)
  1699. fsnotify_create(dir, dentry);
  1700. return error;
  1701. }
  1702. static int may_open(struct path *path, int acc_mode, int flag)
  1703. {
  1704. struct dentry *dentry = path->dentry;
  1705. struct inode *inode = dentry->d_inode;
  1706. int error;
  1707. /* O_PATH? */
  1708. if (!acc_mode)
  1709. return 0;
  1710. if (!inode)
  1711. return -ENOENT;
  1712. switch (inode->i_mode & S_IFMT) {
  1713. case S_IFLNK:
  1714. return -ELOOP;
  1715. case S_IFDIR:
  1716. if (acc_mode & MAY_WRITE)
  1717. return -EISDIR;
  1718. break;
  1719. case S_IFBLK:
  1720. case S_IFCHR:
  1721. if (path->mnt->mnt_flags & MNT_NODEV)
  1722. return -EACCES;
  1723. /*FALLTHRU*/
  1724. case S_IFIFO:
  1725. case S_IFSOCK:
  1726. flag &= ~O_TRUNC;
  1727. break;
  1728. }
  1729. error = inode_permission(inode, acc_mode);
  1730. if (error)
  1731. return error;
  1732. /*
  1733. * An append-only file must be opened in append mode for writing.
  1734. */
  1735. if (IS_APPEND(inode)) {
  1736. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  1737. return -EPERM;
  1738. if (flag & O_TRUNC)
  1739. return -EPERM;
  1740. }
  1741. /* O_NOATIME can only be set by the owner or superuser */
  1742. if (flag & O_NOATIME && !inode_owner_or_capable(inode))
  1743. return -EPERM;
  1744. /*
  1745. * Ensure there are no outstanding leases on the file.
  1746. */
  1747. return break_lease(inode, flag);
  1748. }
  1749. static int handle_truncate(struct file *filp)
  1750. {
  1751. struct path *path = &filp->f_path;
  1752. struct inode *inode = path->dentry->d_inode;
  1753. int error = get_write_access(inode);
  1754. if (error)
  1755. return error;
  1756. /*
  1757. * Refuse to truncate files with mandatory locks held on them.
  1758. */
  1759. error = locks_verify_locked(inode);
  1760. if (!error)
  1761. error = security_path_truncate(path);
  1762. if (!error) {
  1763. error = do_truncate(path->dentry, 0,
  1764. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1765. filp);
  1766. }
  1767. put_write_access(inode);
  1768. return error;
  1769. }
  1770. /*
  1771. * Note that while the flag value (low two bits) for sys_open means:
  1772. * 00 - read-only
  1773. * 01 - write-only
  1774. * 10 - read-write
  1775. * 11 - special
  1776. * it is changed into
  1777. * 00 - no permissions needed
  1778. * 01 - read-permission
  1779. * 10 - write-permission
  1780. * 11 - read-write
  1781. * for the internal routines (ie open_namei()/follow_link() etc)
  1782. * This is more logical, and also allows the 00 "no perm needed"
  1783. * to be used for symlinks (where the permissions are checked
  1784. * later).
  1785. *
  1786. */
  1787. static inline int open_to_namei_flags(int flag)
  1788. {
  1789. if ((flag+1) & O_ACCMODE)
  1790. flag++;
  1791. return flag;
  1792. }
  1793. /*
  1794. * Handle the last step of open()
  1795. */
  1796. static struct file *do_last(struct nameidata *nd, struct path *path,
  1797. const struct open_flags *op, const char *pathname)
  1798. {
  1799. struct dentry *dir = nd->path.dentry;
  1800. struct dentry *dentry;
  1801. int open_flag = op->open_flag;
  1802. int will_truncate = open_flag & O_TRUNC;
  1803. int want_write = 0;
  1804. int acc_mode = op->acc_mode;
  1805. struct file *filp;
  1806. int error;
  1807. nd->flags &= ~LOOKUP_PARENT;
  1808. nd->flags |= op->intent;
  1809. switch (nd->last_type) {
  1810. case LAST_DOTDOT:
  1811. case LAST_DOT:
  1812. error = handle_dots(nd, nd->last_type);
  1813. if (error)
  1814. return ERR_PTR(error);
  1815. /* fallthrough */
  1816. case LAST_ROOT:
  1817. error = complete_walk(nd);
  1818. if (error)
  1819. return ERR_PTR(error);
  1820. audit_inode(pathname, nd->path.dentry);
  1821. if (open_flag & O_CREAT) {
  1822. error = -EISDIR;
  1823. goto exit;
  1824. }
  1825. goto ok;
  1826. case LAST_BIND:
  1827. error = complete_walk(nd);
  1828. if (error)
  1829. return ERR_PTR(error);
  1830. audit_inode(pathname, dir);
  1831. goto ok;
  1832. }
  1833. if (!(open_flag & O_CREAT)) {
  1834. int symlink_ok = 0;
  1835. if (nd->last.name[nd->last.len])
  1836. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1837. if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
  1838. symlink_ok = 1;
  1839. /* we _can_ be in RCU mode here */
  1840. error = walk_component(nd, path, &nd->last, LAST_NORM,
  1841. !symlink_ok);
  1842. if (error < 0)
  1843. return ERR_PTR(error);
  1844. if (error) /* symlink */
  1845. return NULL;
  1846. /* sayonara */
  1847. error = complete_walk(nd);
  1848. if (error)
  1849. return ERR_PTR(-ECHILD);
  1850. error = -ENOTDIR;
  1851. if (nd->flags & LOOKUP_DIRECTORY) {
  1852. if (!nd->inode->i_op->lookup)
  1853. goto exit;
  1854. }
  1855. audit_inode(pathname, nd->path.dentry);
  1856. goto ok;
  1857. }
  1858. /* create side of things */
  1859. error = complete_walk(nd);
  1860. if (error)
  1861. return ERR_PTR(error);
  1862. audit_inode(pathname, dir);
  1863. error = -EISDIR;
  1864. /* trailing slashes? */
  1865. if (nd->last.name[nd->last.len])
  1866. goto exit;
  1867. mutex_lock(&dir->d_inode->i_mutex);
  1868. dentry = lookup_hash(nd);
  1869. error = PTR_ERR(dentry);
  1870. if (IS_ERR(dentry)) {
  1871. mutex_unlock(&dir->d_inode->i_mutex);
  1872. goto exit;
  1873. }
  1874. path->dentry = dentry;
  1875. path->mnt = nd->path.mnt;
  1876. /* Negative dentry, just create the file */
  1877. if (!dentry->d_inode) {
  1878. int mode = op->mode;
  1879. if (!IS_POSIXACL(dir->d_inode))
  1880. mode &= ~current_umask();
  1881. /*
  1882. * This write is needed to ensure that a
  1883. * rw->ro transition does not occur between
  1884. * the time when the file is created and when
  1885. * a permanent write count is taken through
  1886. * the 'struct file' in nameidata_to_filp().
  1887. */
  1888. error = mnt_want_write(nd->path.mnt);
  1889. if (error)
  1890. goto exit_mutex_unlock;
  1891. want_write = 1;
  1892. /* Don't check for write permission, don't truncate */
  1893. open_flag &= ~O_TRUNC;
  1894. will_truncate = 0;
  1895. acc_mode = MAY_OPEN;
  1896. error = security_path_mknod(&nd->path, dentry, mode, 0);
  1897. if (error)
  1898. goto exit_mutex_unlock;
  1899. error = vfs_create(dir->d_inode, dentry, mode, nd);
  1900. if (error)
  1901. goto exit_mutex_unlock;
  1902. mutex_unlock(&dir->d_inode->i_mutex);
  1903. dput(nd->path.dentry);
  1904. nd->path.dentry = dentry;
  1905. goto common;
  1906. }
  1907. /*
  1908. * It already exists.
  1909. */
  1910. mutex_unlock(&dir->d_inode->i_mutex);
  1911. audit_inode(pathname, path->dentry);
  1912. error = -EEXIST;
  1913. if (open_flag & O_EXCL)
  1914. goto exit_dput;
  1915. error = follow_managed(path, nd->flags);
  1916. if (error < 0)
  1917. goto exit_dput;
  1918. error = -ENOENT;
  1919. if (!path->dentry->d_inode)
  1920. goto exit_dput;
  1921. if (path->dentry->d_inode->i_op->follow_link)
  1922. return NULL;
  1923. path_to_nameidata(path, nd);
  1924. nd->inode = path->dentry->d_inode;
  1925. error = -EISDIR;
  1926. if (S_ISDIR(nd->inode->i_mode))
  1927. goto exit;
  1928. ok:
  1929. if (!S_ISREG(nd->inode->i_mode))
  1930. will_truncate = 0;
  1931. if (will_truncate) {
  1932. error = mnt_want_write(nd->path.mnt);
  1933. if (error)
  1934. goto exit;
  1935. want_write = 1;
  1936. }
  1937. common:
  1938. error = may_open(&nd->path, acc_mode, open_flag);
  1939. if (error)
  1940. goto exit;
  1941. filp = nameidata_to_filp(nd);
  1942. if (!IS_ERR(filp)) {
  1943. error = ima_file_check(filp, op->acc_mode);
  1944. if (error) {
  1945. fput(filp);
  1946. filp = ERR_PTR(error);
  1947. }
  1948. }
  1949. if (!IS_ERR(filp)) {
  1950. if (will_truncate) {
  1951. error = handle_truncate(filp);
  1952. if (error) {
  1953. fput(filp);
  1954. filp = ERR_PTR(error);
  1955. }
  1956. }
  1957. }
  1958. out:
  1959. if (want_write)
  1960. mnt_drop_write(nd->path.mnt);
  1961. path_put(&nd->path);
  1962. return filp;
  1963. exit_mutex_unlock:
  1964. mutex_unlock(&dir->d_inode->i_mutex);
  1965. exit_dput:
  1966. path_put_conditional(path, nd);
  1967. exit:
  1968. filp = ERR_PTR(error);
  1969. goto out;
  1970. }
  1971. static struct file *path_openat(int dfd, const char *pathname,
  1972. struct nameidata *nd, const struct open_flags *op, int flags)
  1973. {
  1974. struct file *base = NULL;
  1975. struct file *filp;
  1976. struct path path;
  1977. int error;
  1978. filp = get_empty_filp();
  1979. if (!filp)
  1980. return ERR_PTR(-ENFILE);
  1981. filp->f_flags = op->open_flag;
  1982. nd->intent.open.file = filp;
  1983. nd->intent.open.flags = open_to_namei_flags(op->open_flag);
  1984. nd->intent.open.create_mode = op->mode;
  1985. error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
  1986. if (unlikely(error))
  1987. goto out_filp;
  1988. current->total_link_count = 0;
  1989. error = link_path_walk(pathname, nd);
  1990. if (unlikely(error))
  1991. goto out_filp;
  1992. filp = do_last(nd, &path, op, pathname);
  1993. while (unlikely(!filp)) { /* trailing symlink */
  1994. struct path link = path;
  1995. void *cookie;
  1996. if (!(nd->flags & LOOKUP_FOLLOW)) {
  1997. path_put_conditional(&path, nd);
  1998. path_put(&nd->path);
  1999. filp = ERR_PTR(-ELOOP);
  2000. break;
  2001. }
  2002. nd->flags |= LOOKUP_PARENT;
  2003. nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  2004. error = follow_link(&link, nd, &cookie);
  2005. if (unlikely(error))
  2006. filp = ERR_PTR(error);
  2007. else
  2008. filp = do_last(nd, &path, op, pathname);
  2009. put_link(nd, &link, cookie);
  2010. }
  2011. out:
  2012. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
  2013. path_put(&nd->root);
  2014. if (base)
  2015. fput(base);
  2016. release_open_intent(nd);
  2017. return filp;
  2018. out_filp:
  2019. filp = ERR_PTR(error);
  2020. goto out;
  2021. }
  2022. struct file *do_filp_open(int dfd, const char *pathname,
  2023. const struct open_flags *op, int flags)
  2024. {
  2025. struct nameidata nd;
  2026. struct file *filp;
  2027. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
  2028. if (unlikely(filp == ERR_PTR(-ECHILD)))
  2029. filp = path_openat(dfd, pathname, &nd, op, flags);
  2030. if (unlikely(filp == ERR_PTR(-ESTALE)))
  2031. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
  2032. return filp;
  2033. }
  2034. struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  2035. const char *name, const struct open_flags *op, int flags)
  2036. {
  2037. struct nameidata nd;
  2038. struct file *file;
  2039. nd.root.mnt = mnt;
  2040. nd.root.dentry = dentry;
  2041. flags |= LOOKUP_ROOT;
  2042. if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
  2043. return ERR_PTR(-ELOOP);
  2044. file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
  2045. if (unlikely(file == ERR_PTR(-ECHILD)))
  2046. file = path_openat(-1, name, &nd, op, flags);
  2047. if (unlikely(file == ERR_PTR(-ESTALE)))
  2048. file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
  2049. return file;
  2050. }
  2051. /**
  2052. * lookup_create - lookup a dentry, creating it if it doesn't exist
  2053. * @nd: nameidata info
  2054. * @is_dir: directory flag
  2055. *
  2056. * Simple function to lookup and return a dentry and create it
  2057. * if it doesn't exist. Is SMP-safe.
  2058. *
  2059. * Returns with nd->path.dentry->d_inode->i_mutex locked.
  2060. */
  2061. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  2062. {
  2063. struct dentry *dentry = ERR_PTR(-EEXIST);
  2064. mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2065. /*
  2066. * Yucky last component or no last component at all?
  2067. * (foo/., foo/.., /////)
  2068. */
  2069. if (nd->last_type != LAST_NORM)
  2070. goto fail;
  2071. nd->flags &= ~LOOKUP_PARENT;
  2072. nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  2073. nd->intent.open.flags = O_EXCL;
  2074. /*
  2075. * Do the final lookup.
  2076. */
  2077. dentry = lookup_hash(nd);
  2078. if (IS_ERR(dentry))
  2079. goto fail;
  2080. if (dentry->d_inode)
  2081. goto eexist;
  2082. /*
  2083. * Special case - lookup gave negative, but... we had foo/bar/
  2084. * From the vfs_mknod() POV we just have a negative dentry -
  2085. * all is fine. Let's be bastards - you had / on the end, you've
  2086. * been asking for (non-existent) directory. -ENOENT for you.
  2087. */
  2088. if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
  2089. dput(dentry);
  2090. dentry = ERR_PTR(-ENOENT);
  2091. }
  2092. return dentry;
  2093. eexist:
  2094. dput(dentry);
  2095. dentry = ERR_PTR(-EEXIST);
  2096. fail:
  2097. return dentry;
  2098. }
  2099. EXPORT_SYMBOL_GPL(lookup_create);
  2100. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  2101. {
  2102. int error = may_create(dir, dentry);
  2103. if (error)
  2104. return error;
  2105. if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
  2106. !ns_capable(inode_userns(dir), CAP_MKNOD))
  2107. return -EPERM;
  2108. if (!dir->i_op->mknod)
  2109. return -EPERM;
  2110. error = devcgroup_inode_mknod(mode, dev);
  2111. if (error)
  2112. return error;
  2113. error = security_inode_mknod(dir, dentry, mode, dev);
  2114. if (error)
  2115. return error;
  2116. error = dir->i_op->mknod(dir, dentry, mode, dev);
  2117. if (!error)
  2118. fsnotify_create(dir, dentry);
  2119. return error;
  2120. }
  2121. static int may_mknod(mode_t mode)
  2122. {
  2123. switch (mode & S_IFMT) {
  2124. case S_IFREG:
  2125. case S_IFCHR:
  2126. case S_IFBLK:
  2127. case S_IFIFO:
  2128. case S_IFSOCK:
  2129. case 0: /* zero mode translates to S_IFREG */
  2130. return 0;
  2131. case S_IFDIR:
  2132. return -EPERM;
  2133. default:
  2134. return -EINVAL;
  2135. }
  2136. }
  2137. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
  2138. unsigned, dev)
  2139. {
  2140. int error;
  2141. char *tmp;
  2142. struct dentry *dentry;
  2143. struct nameidata nd;
  2144. if (S_ISDIR(mode))
  2145. return -EPERM;
  2146. error = user_path_parent(dfd, filename, &nd, &tmp);
  2147. if (error)
  2148. return error;
  2149. dentry = lookup_create(&nd, 0);
  2150. if (IS_ERR(dentry)) {
  2151. error = PTR_ERR(dentry);
  2152. goto out_unlock;
  2153. }
  2154. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  2155. mode &= ~current_umask();
  2156. error = may_mknod(mode);
  2157. if (error)
  2158. goto out_dput;
  2159. error = mnt_want_write(nd.path.mnt);
  2160. if (error)
  2161. goto out_dput;
  2162. error = security_path_mknod(&nd.path, dentry, mode, dev);
  2163. if (error)
  2164. goto out_drop_write;
  2165. switch (mode & S_IFMT) {
  2166. case 0: case S_IFREG:
  2167. error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
  2168. break;
  2169. case S_IFCHR: case S_IFBLK:
  2170. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
  2171. new_decode_dev(dev));
  2172. break;
  2173. case S_IFIFO: case S_IFSOCK:
  2174. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
  2175. break;
  2176. }
  2177. out_drop_write:
  2178. mnt_drop_write(nd.path.mnt);
  2179. out_dput:
  2180. dput(dentry);
  2181. out_unlock:
  2182. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2183. path_put(&nd.path);
  2184. putname(tmp);
  2185. return error;
  2186. }
  2187. SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
  2188. {
  2189. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  2190. }
  2191. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  2192. {
  2193. int error = may_create(dir, dentry);
  2194. if (error)
  2195. return error;
  2196. if (!dir->i_op->mkdir)
  2197. return -EPERM;
  2198. mode &= (S_IRWXUGO|S_ISVTX);
  2199. error = security_inode_mkdir(dir, dentry, mode);
  2200. if (error)
  2201. return error;
  2202. error = dir->i_op->mkdir(dir, dentry, mode);
  2203. if (!error)
  2204. fsnotify_mkdir(dir, dentry);
  2205. return error;
  2206. }
  2207. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
  2208. {
  2209. int error = 0;
  2210. char * tmp;
  2211. struct dentry *dentry;
  2212. struct nameidata nd;
  2213. error = user_path_parent(dfd, pathname, &nd, &tmp);
  2214. if (error)
  2215. goto out_err;
  2216. dentry = lookup_create(&nd, 1);
  2217. error = PTR_ERR(dentry);
  2218. if (IS_ERR(dentry))
  2219. goto out_unlock;
  2220. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  2221. mode &= ~current_umask();
  2222. error = mnt_want_write(nd.path.mnt);
  2223. if (error)
  2224. goto out_dput;
  2225. error = security_path_mkdir(&nd.path, dentry, mode);
  2226. if (error)
  2227. goto out_drop_write;
  2228. error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
  2229. out_drop_write:
  2230. mnt_drop_write(nd.path.mnt);
  2231. out_dput:
  2232. dput(dentry);
  2233. out_unlock:
  2234. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2235. path_put(&nd.path);
  2236. putname(tmp);
  2237. out_err:
  2238. return error;
  2239. }
  2240. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
  2241. {
  2242. return sys_mkdirat(AT_FDCWD, pathname, mode);
  2243. }
  2244. /*
  2245. * The dentry_unhash() helper will try to drop the dentry early: we
  2246. * should have a usage count of 2 if we're the only user of this
  2247. * dentry, and if that is true (possibly after pruning the dcache),
  2248. * then we drop the dentry now.
  2249. *
  2250. * A low-level filesystem can, if it choses, legally
  2251. * do a
  2252. *
  2253. * if (!d_unhashed(dentry))
  2254. * return -EBUSY;
  2255. *
  2256. * if it cannot handle the case of removing a directory
  2257. * that is still in use by something else..
  2258. */
  2259. void dentry_unhash(struct dentry *dentry)
  2260. {
  2261. shrink_dcache_parent(dentry);
  2262. spin_lock(&dentry->d_lock);
  2263. if (dentry->d_count == 1)
  2264. __d_drop(dentry);
  2265. spin_unlock(&dentry->d_lock);
  2266. }
  2267. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  2268. {
  2269. int error = may_delete(dir, dentry, 1);
  2270. if (error)
  2271. return error;
  2272. if (!dir->i_op->rmdir)
  2273. return -EPERM;
  2274. mutex_lock(&dentry->d_inode->i_mutex);
  2275. error = -EBUSY;
  2276. if (d_mountpoint(dentry))
  2277. goto out;
  2278. error = security_inode_rmdir(dir, dentry);
  2279. if (error)
  2280. goto out;
  2281. error = dir->i_op->rmdir(dir, dentry);
  2282. if (error)
  2283. goto out;
  2284. dentry->d_inode->i_flags |= S_DEAD;
  2285. dont_mount(dentry);
  2286. out:
  2287. mutex_unlock(&dentry->d_inode->i_mutex);
  2288. if (!error)
  2289. d_delete(dentry);
  2290. return error;
  2291. }
  2292. static long do_rmdir(int dfd, const char __user *pathname)
  2293. {
  2294. int error = 0;
  2295. char * name;
  2296. struct dentry *dentry;
  2297. struct nameidata nd;
  2298. error = user_path_parent(dfd, pathname, &nd, &name);
  2299. if (error)
  2300. return error;
  2301. switch(nd.last_type) {
  2302. case LAST_DOTDOT:
  2303. error = -ENOTEMPTY;
  2304. goto exit1;
  2305. case LAST_DOT:
  2306. error = -EINVAL;
  2307. goto exit1;
  2308. case LAST_ROOT:
  2309. error = -EBUSY;
  2310. goto exit1;
  2311. }
  2312. nd.flags &= ~LOOKUP_PARENT;
  2313. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2314. dentry = lookup_hash(&nd);
  2315. error = PTR_ERR(dentry);
  2316. if (IS_ERR(dentry))
  2317. goto exit2;
  2318. error = mnt_want_write(nd.path.mnt);
  2319. if (error)
  2320. goto exit3;
  2321. error = security_path_rmdir(&nd.path, dentry);
  2322. if (error)
  2323. goto exit4;
  2324. error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
  2325. exit4:
  2326. mnt_drop_write(nd.path.mnt);
  2327. exit3:
  2328. dput(dentry);
  2329. exit2:
  2330. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2331. exit1:
  2332. path_put(&nd.path);
  2333. putname(name);
  2334. return error;
  2335. }
  2336. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  2337. {
  2338. return do_rmdir(AT_FDCWD, pathname);
  2339. }
  2340. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  2341. {
  2342. int error = may_delete(dir, dentry, 0);
  2343. if (error)
  2344. return error;
  2345. if (!dir->i_op->unlink)
  2346. return -EPERM;
  2347. mutex_lock(&dentry->d_inode->i_mutex);
  2348. if (d_mountpoint(dentry))
  2349. error = -EBUSY;
  2350. else {
  2351. error = security_inode_unlink(dir, dentry);
  2352. if (!error) {
  2353. error = dir->i_op->unlink(dir, dentry);
  2354. if (!error)
  2355. dont_mount(dentry);
  2356. }
  2357. }
  2358. mutex_unlock(&dentry->d_inode->i_mutex);
  2359. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  2360. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  2361. fsnotify_link_count(dentry->d_inode);
  2362. d_delete(dentry);
  2363. }
  2364. return error;
  2365. }
  2366. /*
  2367. * Make sure that the actual truncation of the file will occur outside its
  2368. * directory's i_mutex. Truncate can take a long time if there is a lot of
  2369. * writeout happening, and we don't want to prevent access to the directory
  2370. * while waiting on the I/O.
  2371. */
  2372. static long do_unlinkat(int dfd, const char __user *pathname)
  2373. {
  2374. int error;
  2375. char *name;
  2376. struct dentry *dentry;
  2377. struct nameidata nd;
  2378. struct inode *inode = NULL;
  2379. error = user_path_parent(dfd, pathname, &nd, &name);
  2380. if (error)
  2381. return error;
  2382. error = -EISDIR;
  2383. if (nd.last_type != LAST_NORM)
  2384. goto exit1;
  2385. nd.flags &= ~LOOKUP_PARENT;
  2386. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2387. dentry = lookup_hash(&nd);
  2388. error = PTR_ERR(dentry);
  2389. if (!IS_ERR(dentry)) {
  2390. /* Why not before? Because we want correct error value */
  2391. if (nd.last.name[nd.last.len])
  2392. goto slashes;
  2393. inode = dentry->d_inode;
  2394. if (inode)
  2395. ihold(inode);
  2396. error = mnt_want_write(nd.path.mnt);
  2397. if (error)
  2398. goto exit2;
  2399. error = security_path_unlink(&nd.path, dentry);
  2400. if (error)
  2401. goto exit3;
  2402. error = vfs_unlink(nd.path.dentry->d_inode, dentry);
  2403. exit3:
  2404. mnt_drop_write(nd.path.mnt);
  2405. exit2:
  2406. dput(dentry);
  2407. }
  2408. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2409. if (inode)
  2410. iput(inode); /* truncate the inode here */
  2411. exit1:
  2412. path_put(&nd.path);
  2413. putname(name);
  2414. return error;
  2415. slashes:
  2416. error = !dentry->d_inode ? -ENOENT :
  2417. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  2418. goto exit2;
  2419. }
  2420. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  2421. {
  2422. if ((flag & ~AT_REMOVEDIR) != 0)
  2423. return -EINVAL;
  2424. if (flag & AT_REMOVEDIR)
  2425. return do_rmdir(dfd, pathname);
  2426. return do_unlinkat(dfd, pathname);
  2427. }
  2428. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  2429. {
  2430. return do_unlinkat(AT_FDCWD, pathname);
  2431. }
  2432. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  2433. {
  2434. int error = may_create(dir, dentry);
  2435. if (error)
  2436. return error;
  2437. if (!dir->i_op->symlink)
  2438. return -EPERM;
  2439. error = security_inode_symlink(dir, dentry, oldname);
  2440. if (error)
  2441. return error;
  2442. error = dir->i_op->symlink(dir, dentry, oldname);
  2443. if (!error)
  2444. fsnotify_create(dir, dentry);
  2445. return error;
  2446. }
  2447. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  2448. int, newdfd, const char __user *, newname)
  2449. {
  2450. int error;
  2451. char *from;
  2452. char *to;
  2453. struct dentry *dentry;
  2454. struct nameidata nd;
  2455. from = getname(oldname);
  2456. if (IS_ERR(from))
  2457. return PTR_ERR(from);
  2458. error = user_path_parent(newdfd, newname, &nd, &to);
  2459. if (error)
  2460. goto out_putname;
  2461. dentry = lookup_create(&nd, 0);
  2462. error = PTR_ERR(dentry);
  2463. if (IS_ERR(dentry))
  2464. goto out_unlock;
  2465. error = mnt_want_write(nd.path.mnt);
  2466. if (error)
  2467. goto out_dput;
  2468. error = security_path_symlink(&nd.path, dentry, from);
  2469. if (error)
  2470. goto out_drop_write;
  2471. error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
  2472. out_drop_write:
  2473. mnt_drop_write(nd.path.mnt);
  2474. out_dput:
  2475. dput(dentry);
  2476. out_unlock:
  2477. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2478. path_put(&nd.path);
  2479. putname(to);
  2480. out_putname:
  2481. putname(from);
  2482. return error;
  2483. }
  2484. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  2485. {
  2486. return sys_symlinkat(oldname, AT_FDCWD, newname);
  2487. }
  2488. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2489. {
  2490. struct inode *inode = old_dentry->d_inode;
  2491. int error;
  2492. if (!inode)
  2493. return -ENOENT;
  2494. error = may_create(dir, new_dentry);
  2495. if (error)
  2496. return error;
  2497. if (dir->i_sb != inode->i_sb)
  2498. return -EXDEV;
  2499. /*
  2500. * A link to an append-only or immutable file cannot be created.
  2501. */
  2502. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2503. return -EPERM;
  2504. if (!dir->i_op->link)
  2505. return -EPERM;
  2506. if (S_ISDIR(inode->i_mode))
  2507. return -EPERM;
  2508. error = security_inode_link(old_dentry, dir, new_dentry);
  2509. if (error)
  2510. return error;
  2511. mutex_lock(&inode->i_mutex);
  2512. /* Make sure we don't allow creating hardlink to an unlinked file */
  2513. if (inode->i_nlink == 0)
  2514. error = -ENOENT;
  2515. else
  2516. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2517. mutex_unlock(&inode->i_mutex);
  2518. if (!error)
  2519. fsnotify_link(dir, inode, new_dentry);
  2520. return error;
  2521. }
  2522. /*
  2523. * Hardlinks are often used in delicate situations. We avoid
  2524. * security-related surprises by not following symlinks on the
  2525. * newname. --KAB
  2526. *
  2527. * We don't follow them on the oldname either to be compatible
  2528. * with linux 2.0, and to avoid hard-linking to directories
  2529. * and other special files. --ADM
  2530. */
  2531. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  2532. int, newdfd, const char __user *, newname, int, flags)
  2533. {
  2534. struct dentry *new_dentry;
  2535. struct nameidata nd;
  2536. struct path old_path;
  2537. int how = 0;
  2538. int error;
  2539. char *to;
  2540. if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  2541. return -EINVAL;
  2542. /*
  2543. * To use null names we require CAP_DAC_READ_SEARCH
  2544. * This ensures that not everyone will be able to create
  2545. * handlink using the passed filedescriptor.
  2546. */
  2547. if (flags & AT_EMPTY_PATH) {
  2548. if (!capable(CAP_DAC_READ_SEARCH))
  2549. return -ENOENT;
  2550. how = LOOKUP_EMPTY;
  2551. }
  2552. if (flags & AT_SYMLINK_FOLLOW)
  2553. how |= LOOKUP_FOLLOW;
  2554. error = user_path_at(olddfd, oldname, how, &old_path);
  2555. if (error)
  2556. return error;
  2557. error = user_path_parent(newdfd, newname, &nd, &to);
  2558. if (error)
  2559. goto out;
  2560. error = -EXDEV;
  2561. if (old_path.mnt != nd.path.mnt)
  2562. goto out_release;
  2563. new_dentry = lookup_create(&nd, 0);
  2564. error = PTR_ERR(new_dentry);
  2565. if (IS_ERR(new_dentry))
  2566. goto out_unlock;
  2567. error = mnt_want_write(nd.path.mnt);
  2568. if (error)
  2569. goto out_dput;
  2570. error = security_path_link(old_path.dentry, &nd.path, new_dentry);
  2571. if (error)
  2572. goto out_drop_write;
  2573. error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
  2574. out_drop_write:
  2575. mnt_drop_write(nd.path.mnt);
  2576. out_dput:
  2577. dput(new_dentry);
  2578. out_unlock:
  2579. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2580. out_release:
  2581. path_put(&nd.path);
  2582. putname(to);
  2583. out:
  2584. path_put(&old_path);
  2585. return error;
  2586. }
  2587. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  2588. {
  2589. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2590. }
  2591. /*
  2592. * The worst of all namespace operations - renaming directory. "Perverted"
  2593. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2594. * Problems:
  2595. * a) we can get into loop creation. Check is done in is_subdir().
  2596. * b) race potential - two innocent renames can create a loop together.
  2597. * That's where 4.4 screws up. Current fix: serialization on
  2598. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2599. * story.
  2600. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2601. * And that - after we got ->i_mutex on parents (until then we don't know
  2602. * whether the target exists). Solution: try to be smart with locking
  2603. * order for inodes. We rely on the fact that tree topology may change
  2604. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2605. * move will be locked. Thus we can rank directories by the tree
  2606. * (ancestors first) and rank all non-directories after them.
  2607. * That works since everybody except rename does "lock parent, lookup,
  2608. * lock child" and rename is under ->s_vfs_rename_mutex.
  2609. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2610. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2611. * we'd better make sure that there's no link(2) for them.
  2612. * d) conversion from fhandle to dentry may come in the wrong moment - when
  2613. * we are removing the target. Solution: we will have to grab ->i_mutex
  2614. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2615. * ->i_mutex on parents, which works but leads to some truly excessive
  2616. * locking].
  2617. */
  2618. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2619. struct inode *new_dir, struct dentry *new_dentry)
  2620. {
  2621. int error = 0;
  2622. struct inode *target = new_dentry->d_inode;
  2623. /*
  2624. * If we are going to change the parent - check write permissions,
  2625. * we'll need to flip '..'.
  2626. */
  2627. if (new_dir != old_dir) {
  2628. error = inode_permission(old_dentry->d_inode, MAY_WRITE);
  2629. if (error)
  2630. return error;
  2631. }
  2632. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2633. if (error)
  2634. return error;
  2635. if (target)
  2636. mutex_lock(&target->i_mutex);
  2637. error = -EBUSY;
  2638. if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
  2639. goto out;
  2640. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2641. if (error)
  2642. goto out;
  2643. if (target) {
  2644. target->i_flags |= S_DEAD;
  2645. dont_mount(new_dentry);
  2646. }
  2647. out:
  2648. if (target)
  2649. mutex_unlock(&target->i_mutex);
  2650. if (!error)
  2651. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2652. d_move(old_dentry,new_dentry);
  2653. return error;
  2654. }
  2655. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2656. struct inode *new_dir, struct dentry *new_dentry)
  2657. {
  2658. struct inode *target = new_dentry->d_inode;
  2659. int error;
  2660. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2661. if (error)
  2662. return error;
  2663. dget(new_dentry);
  2664. if (target)
  2665. mutex_lock(&target->i_mutex);
  2666. error = -EBUSY;
  2667. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2668. goto out;
  2669. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2670. if (error)
  2671. goto out;
  2672. if (target)
  2673. dont_mount(new_dentry);
  2674. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2675. d_move(old_dentry, new_dentry);
  2676. out:
  2677. if (target)
  2678. mutex_unlock(&target->i_mutex);
  2679. dput(new_dentry);
  2680. return error;
  2681. }
  2682. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2683. struct inode *new_dir, struct dentry *new_dentry)
  2684. {
  2685. int error;
  2686. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2687. const unsigned char *old_name;
  2688. if (old_dentry->d_inode == new_dentry->d_inode)
  2689. return 0;
  2690. error = may_delete(old_dir, old_dentry, is_dir);
  2691. if (error)
  2692. return error;
  2693. if (!new_dentry->d_inode)
  2694. error = may_create(new_dir, new_dentry);
  2695. else
  2696. error = may_delete(new_dir, new_dentry, is_dir);
  2697. if (error)
  2698. return error;
  2699. if (!old_dir->i_op->rename)
  2700. return -EPERM;
  2701. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2702. if (is_dir)
  2703. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2704. else
  2705. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2706. if (!error)
  2707. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  2708. new_dentry->d_inode, old_dentry);
  2709. fsnotify_oldname_free(old_name);
  2710. return error;
  2711. }
  2712. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  2713. int, newdfd, const char __user *, newname)
  2714. {
  2715. struct dentry *old_dir, *new_dir;
  2716. struct dentry *old_dentry, *new_dentry;
  2717. struct dentry *trap;
  2718. struct nameidata oldnd, newnd;
  2719. char *from;
  2720. char *to;
  2721. int error;
  2722. error = user_path_parent(olddfd, oldname, &oldnd, &from);
  2723. if (error)
  2724. goto exit;
  2725. error = user_path_parent(newdfd, newname, &newnd, &to);
  2726. if (error)
  2727. goto exit1;
  2728. error = -EXDEV;
  2729. if (oldnd.path.mnt != newnd.path.mnt)
  2730. goto exit2;
  2731. old_dir = oldnd.path.dentry;
  2732. error = -EBUSY;
  2733. if (oldnd.last_type != LAST_NORM)
  2734. goto exit2;
  2735. new_dir = newnd.path.dentry;
  2736. if (newnd.last_type != LAST_NORM)
  2737. goto exit2;
  2738. oldnd.flags &= ~LOOKUP_PARENT;
  2739. newnd.flags &= ~LOOKUP_PARENT;
  2740. newnd.flags |= LOOKUP_RENAME_TARGET;
  2741. trap = lock_rename(new_dir, old_dir);
  2742. old_dentry = lookup_hash(&oldnd);
  2743. error = PTR_ERR(old_dentry);
  2744. if (IS_ERR(old_dentry))
  2745. goto exit3;
  2746. /* source must exist */
  2747. error = -ENOENT;
  2748. if (!old_dentry->d_inode)
  2749. goto exit4;
  2750. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2751. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2752. error = -ENOTDIR;
  2753. if (oldnd.last.name[oldnd.last.len])
  2754. goto exit4;
  2755. if (newnd.last.name[newnd.last.len])
  2756. goto exit4;
  2757. }
  2758. /* source should not be ancestor of target */
  2759. error = -EINVAL;
  2760. if (old_dentry == trap)
  2761. goto exit4;
  2762. new_dentry = lookup_hash(&newnd);
  2763. error = PTR_ERR(new_dentry);
  2764. if (IS_ERR(new_dentry))
  2765. goto exit4;
  2766. /* target should not be an ancestor of source */
  2767. error = -ENOTEMPTY;
  2768. if (new_dentry == trap)
  2769. goto exit5;
  2770. error = mnt_want_write(oldnd.path.mnt);
  2771. if (error)
  2772. goto exit5;
  2773. error = security_path_rename(&oldnd.path, old_dentry,
  2774. &newnd.path, new_dentry);
  2775. if (error)
  2776. goto exit6;
  2777. error = vfs_rename(old_dir->d_inode, old_dentry,
  2778. new_dir->d_inode, new_dentry);
  2779. exit6:
  2780. mnt_drop_write(oldnd.path.mnt);
  2781. exit5:
  2782. dput(new_dentry);
  2783. exit4:
  2784. dput(old_dentry);
  2785. exit3:
  2786. unlock_rename(new_dir, old_dir);
  2787. exit2:
  2788. path_put(&newnd.path);
  2789. putname(to);
  2790. exit1:
  2791. path_put(&oldnd.path);
  2792. putname(from);
  2793. exit:
  2794. return error;
  2795. }
  2796. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  2797. {
  2798. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2799. }
  2800. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2801. {
  2802. int len;
  2803. len = PTR_ERR(link);
  2804. if (IS_ERR(link))
  2805. goto out;
  2806. len = strlen(link);
  2807. if (len > (unsigned) buflen)
  2808. len = buflen;
  2809. if (copy_to_user(buffer, link, len))
  2810. len = -EFAULT;
  2811. out:
  2812. return len;
  2813. }
  2814. /*
  2815. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2816. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2817. * using) it for any given inode is up to filesystem.
  2818. */
  2819. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2820. {
  2821. struct nameidata nd;
  2822. void *cookie;
  2823. int res;
  2824. nd.depth = 0;
  2825. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2826. if (IS_ERR(cookie))
  2827. return PTR_ERR(cookie);
  2828. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2829. if (dentry->d_inode->i_op->put_link)
  2830. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2831. return res;
  2832. }
  2833. int vfs_follow_link(struct nameidata *nd, const char *link)
  2834. {
  2835. return __vfs_follow_link(nd, link);
  2836. }
  2837. /* get the link contents into pagecache */
  2838. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2839. {
  2840. char *kaddr;
  2841. struct page *page;
  2842. struct address_space *mapping = dentry->d_inode->i_mapping;
  2843. page = read_mapping_page(mapping, 0, NULL);
  2844. if (IS_ERR(page))
  2845. return (char*)page;
  2846. *ppage = page;
  2847. kaddr = kmap(page);
  2848. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  2849. return kaddr;
  2850. }
  2851. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2852. {
  2853. struct page *page = NULL;
  2854. char *s = page_getlink(dentry, &page);
  2855. int res = vfs_readlink(dentry,buffer,buflen,s);
  2856. if (page) {
  2857. kunmap(page);
  2858. page_cache_release(page);
  2859. }
  2860. return res;
  2861. }
  2862. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2863. {
  2864. struct page *page = NULL;
  2865. nd_set_link(nd, page_getlink(dentry, &page));
  2866. return page;
  2867. }
  2868. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2869. {
  2870. struct page *page = cookie;
  2871. if (page) {
  2872. kunmap(page);
  2873. page_cache_release(page);
  2874. }
  2875. }
  2876. /*
  2877. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  2878. */
  2879. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  2880. {
  2881. struct address_space *mapping = inode->i_mapping;
  2882. struct page *page;
  2883. void *fsdata;
  2884. int err;
  2885. char *kaddr;
  2886. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  2887. if (nofs)
  2888. flags |= AOP_FLAG_NOFS;
  2889. retry:
  2890. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  2891. flags, &page, &fsdata);
  2892. if (err)
  2893. goto fail;
  2894. kaddr = kmap_atomic(page, KM_USER0);
  2895. memcpy(kaddr, symname, len-1);
  2896. kunmap_atomic(kaddr, KM_USER0);
  2897. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  2898. page, fsdata);
  2899. if (err < 0)
  2900. goto fail;
  2901. if (err < len-1)
  2902. goto retry;
  2903. mark_inode_dirty(inode);
  2904. return 0;
  2905. fail:
  2906. return err;
  2907. }
  2908. int page_symlink(struct inode *inode, const char *symname, int len)
  2909. {
  2910. return __page_symlink(inode, symname, len,
  2911. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  2912. }
  2913. const struct inode_operations page_symlink_inode_operations = {
  2914. .readlink = generic_readlink,
  2915. .follow_link = page_follow_link_light,
  2916. .put_link = page_put_link,
  2917. };
  2918. EXPORT_SYMBOL(user_path_at);
  2919. EXPORT_SYMBOL(follow_down_one);
  2920. EXPORT_SYMBOL(follow_down);
  2921. EXPORT_SYMBOL(follow_up);
  2922. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2923. EXPORT_SYMBOL(getname);
  2924. EXPORT_SYMBOL(lock_rename);
  2925. EXPORT_SYMBOL(lookup_one_len);
  2926. EXPORT_SYMBOL(page_follow_link_light);
  2927. EXPORT_SYMBOL(page_put_link);
  2928. EXPORT_SYMBOL(page_readlink);
  2929. EXPORT_SYMBOL(__page_symlink);
  2930. EXPORT_SYMBOL(page_symlink);
  2931. EXPORT_SYMBOL(page_symlink_inode_operations);
  2932. EXPORT_SYMBOL(kern_path_parent);
  2933. EXPORT_SYMBOL(kern_path);
  2934. EXPORT_SYMBOL(vfs_path_lookup);
  2935. EXPORT_SYMBOL(inode_permission);
  2936. EXPORT_SYMBOL(file_permission);
  2937. EXPORT_SYMBOL(unlock_rename);
  2938. EXPORT_SYMBOL(vfs_create);
  2939. EXPORT_SYMBOL(vfs_follow_link);
  2940. EXPORT_SYMBOL(vfs_link);
  2941. EXPORT_SYMBOL(vfs_mkdir);
  2942. EXPORT_SYMBOL(vfs_mknod);
  2943. EXPORT_SYMBOL(generic_permission);
  2944. EXPORT_SYMBOL(vfs_readlink);
  2945. EXPORT_SYMBOL(vfs_rename);
  2946. EXPORT_SYMBOL(vfs_rmdir);
  2947. EXPORT_SYMBOL(vfs_symlink);
  2948. EXPORT_SYMBOL(vfs_unlink);
  2949. EXPORT_SYMBOL(dentry_unhash);
  2950. EXPORT_SYMBOL(generic_readlink);