namei.c 98 KB

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