namei.c 82 KB

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