namei.c 97 KB

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