namei.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018
  1. /*
  2. * linux/fs/namei.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * Some corrections by tytso.
  8. */
  9. /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
  10. * lookup logic.
  11. */
  12. /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/fs.h>
  18. #include <linux/namei.h>
  19. #include <linux/quotaops.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/fsnotify.h>
  22. #include <linux/personality.h>
  23. #include <linux/security.h>
  24. #include <linux/syscalls.h>
  25. #include <linux/mount.h>
  26. #include <linux/audit.h>
  27. #include <linux/capability.h>
  28. #include <linux/file.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/device_cgroup.h>
  31. #include <asm/namei.h>
  32. #include <asm/uaccess.h>
  33. #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
  34. /* [Feb-1997 T. Schoebel-Theuer]
  35. * Fundamental changes in the pathname lookup mechanisms (namei)
  36. * were necessary because of omirr. The reason is that omirr needs
  37. * to know the _real_ pathname, not the user-supplied one, in case
  38. * of symlinks (and also when transname replacements occur).
  39. *
  40. * The new code replaces the old recursive symlink resolution with
  41. * an iterative one (in case of non-nested symlink chains). It does
  42. * this with calls to <fs>_follow_link().
  43. * As a side effect, dir_namei(), _namei() and follow_link() are now
  44. * replaced with a single function lookup_dentry() that can handle all
  45. * the special cases of the former code.
  46. *
  47. * With the new dcache, the pathname is stored at each inode, at least as
  48. * long as the refcount of the inode is positive. As a side effect, the
  49. * size of the dcache depends on the inode cache and thus is dynamic.
  50. *
  51. * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  52. * resolution to correspond with current state of the code.
  53. *
  54. * Note that the symlink resolution is not *completely* iterative.
  55. * There is still a significant amount of tail- and mid- recursion in
  56. * the algorithm. Also, note that <fs>_readlink() is not used in
  57. * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  58. * may return different results than <fs>_follow_link(). Many virtual
  59. * filesystems (including /proc) exhibit this behavior.
  60. */
  61. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  62. * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  63. * and the name already exists in form of a symlink, try to create the new
  64. * name indicated by the symlink. The old code always complained that the
  65. * name already exists, due to not following the symlink even if its target
  66. * is nonexistent. The new semantics affects also mknod() and link() when
  67. * the name is a symlink pointing to a non-existant name.
  68. *
  69. * I don't know which semantics is the right one, since I have no access
  70. * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  71. * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  72. * "old" one. Personally, I think the new semantics is much more logical.
  73. * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  74. * file does succeed in both HP-UX and SunOs, but not in Solaris
  75. * and in the old Linux semantics.
  76. */
  77. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  78. * semantics. See the comments in "open_namei" and "do_link" below.
  79. *
  80. * [10-Sep-98 Alan Modra] Another symlink change.
  81. */
  82. /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  83. * inside the path - always follow.
  84. * in the last component in creation/removal/renaming - never follow.
  85. * if LOOKUP_FOLLOW passed - follow.
  86. * if the pathname has trailing slashes - follow.
  87. * otherwise - don't follow.
  88. * (applied in that order).
  89. *
  90. * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  91. * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  92. * During the 2.4 we need to fix the userland stuff depending on it -
  93. * hopefully we will be able to get rid of that wart in 2.5. So far only
  94. * XEmacs seems to be relying on it...
  95. */
  96. /*
  97. * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
  98. * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
  99. * any extra contention...
  100. */
  101. static int __link_path_walk(const char *name, struct nameidata *nd);
  102. /* In order to reduce some races, while at the same time doing additional
  103. * checking and hopefully speeding things up, we copy filenames to the
  104. * kernel data space before using them..
  105. *
  106. * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
  107. * PATH_MAX includes the nul terminator --RR.
  108. */
  109. static int do_getname(const char __user *filename, char *page)
  110. {
  111. int retval;
  112. unsigned long len = PATH_MAX;
  113. if (!segment_eq(get_fs(), KERNEL_DS)) {
  114. if ((unsigned long) filename >= TASK_SIZE)
  115. return -EFAULT;
  116. if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
  117. len = TASK_SIZE - (unsigned long) filename;
  118. }
  119. retval = strncpy_from_user(page, filename, len);
  120. if (retval > 0) {
  121. if (retval < len)
  122. return 0;
  123. return -ENAMETOOLONG;
  124. } else if (!retval)
  125. retval = -ENOENT;
  126. return retval;
  127. }
  128. char * getname(const char __user * filename)
  129. {
  130. char *tmp, *result;
  131. result = ERR_PTR(-ENOMEM);
  132. tmp = __getname();
  133. if (tmp) {
  134. int retval = do_getname(filename, tmp);
  135. result = tmp;
  136. if (retval < 0) {
  137. __putname(tmp);
  138. result = ERR_PTR(retval);
  139. }
  140. }
  141. audit_getname(result);
  142. return result;
  143. }
  144. #ifdef CONFIG_AUDITSYSCALL
  145. void putname(const char *name)
  146. {
  147. if (unlikely(!audit_dummy_context()))
  148. audit_putname(name);
  149. else
  150. __putname(name);
  151. }
  152. EXPORT_SYMBOL(putname);
  153. #endif
  154. /**
  155. * generic_permission - check for access rights on a Posix-like filesystem
  156. * @inode: inode to check access rights for
  157. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  158. * @check_acl: optional callback to check for Posix ACLs
  159. *
  160. * Used to check for read/write/execute permissions on a file.
  161. * We use "fsuid" for this, letting us set arbitrary permissions
  162. * for filesystem access without changing the "normal" uids which
  163. * are used for other things..
  164. */
  165. int generic_permission(struct inode *inode, int mask,
  166. int (*check_acl)(struct inode *inode, int mask))
  167. {
  168. umode_t mode = inode->i_mode;
  169. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  170. if (current->fsuid == inode->i_uid)
  171. mode >>= 6;
  172. else {
  173. if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
  174. int error = check_acl(inode, mask);
  175. if (error == -EACCES)
  176. goto check_capabilities;
  177. else if (error != -EAGAIN)
  178. return error;
  179. }
  180. if (in_group_p(inode->i_gid))
  181. mode >>= 3;
  182. }
  183. /*
  184. * If the DACs are ok we don't need any capability check.
  185. */
  186. if ((mask & ~mode) == 0)
  187. return 0;
  188. check_capabilities:
  189. /*
  190. * Read/write DACs are always overridable.
  191. * Executable DACs are overridable if at least one exec bit is set.
  192. */
  193. if (!(mask & MAY_EXEC) ||
  194. (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
  195. if (capable(CAP_DAC_OVERRIDE))
  196. return 0;
  197. /*
  198. * Searching includes executable on directories, else just read.
  199. */
  200. if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
  201. if (capable(CAP_DAC_READ_SEARCH))
  202. return 0;
  203. return -EACCES;
  204. }
  205. int permission(struct inode *inode, int mask, struct nameidata *nd)
  206. {
  207. int retval;
  208. struct vfsmount *mnt = NULL;
  209. if (nd)
  210. mnt = nd->path.mnt;
  211. if (mask & MAY_WRITE) {
  212. umode_t mode = inode->i_mode;
  213. /*
  214. * Nobody gets write access to a read-only fs.
  215. */
  216. if (IS_RDONLY(inode) &&
  217. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  218. return -EROFS;
  219. /*
  220. * Nobody gets write access to an immutable file.
  221. */
  222. if (IS_IMMUTABLE(inode))
  223. return -EACCES;
  224. }
  225. if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
  226. /*
  227. * MAY_EXEC on regular files is denied if the fs is mounted
  228. * with the "noexec" flag.
  229. */
  230. if (mnt && (mnt->mnt_flags & MNT_NOEXEC))
  231. return -EACCES;
  232. }
  233. /* Ordinary permission routines do not understand MAY_APPEND. */
  234. if (inode->i_op && inode->i_op->permission) {
  235. int extra = 0;
  236. if (nd) {
  237. if (nd->flags & LOOKUP_ACCESS)
  238. extra |= MAY_ACCESS;
  239. if (nd->flags & LOOKUP_OPEN)
  240. extra |= MAY_OPEN;
  241. }
  242. retval = inode->i_op->permission(inode, mask | extra);
  243. if (!retval) {
  244. /*
  245. * Exec permission on a regular file is denied if none
  246. * of the execute bits are set.
  247. *
  248. * This check should be done by the ->permission()
  249. * method.
  250. */
  251. if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode) &&
  252. !(inode->i_mode & S_IXUGO))
  253. return -EACCES;
  254. }
  255. } else {
  256. retval = generic_permission(inode, mask, NULL);
  257. }
  258. if (retval)
  259. return retval;
  260. retval = devcgroup_inode_permission(inode, mask);
  261. if (retval)
  262. return retval;
  263. return security_inode_permission(inode,
  264. mask & (MAY_READ|MAY_WRITE|MAY_EXEC), nd);
  265. }
  266. /**
  267. * vfs_permission - check for access rights to a given path
  268. * @nd: lookup result that describes the path
  269. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  270. *
  271. * Used to check for read/write/execute permissions on a path.
  272. * We use "fsuid" for this, letting us set arbitrary permissions
  273. * for filesystem access without changing the "normal" uids which
  274. * are used for other things.
  275. */
  276. int vfs_permission(struct nameidata *nd, int mask)
  277. {
  278. return permission(nd->path.dentry->d_inode, mask, nd);
  279. }
  280. /**
  281. * file_permission - check for additional access rights to a given file
  282. * @file: file to check access rights for
  283. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  284. *
  285. * Used to check for read/write/execute permissions on an already opened
  286. * file.
  287. *
  288. * Note:
  289. * Do not use this function in new code. All access checks should
  290. * be done using vfs_permission().
  291. */
  292. int file_permission(struct file *file, int mask)
  293. {
  294. return permission(file->f_path.dentry->d_inode, mask, NULL);
  295. }
  296. /*
  297. * get_write_access() gets write permission for a file.
  298. * put_write_access() releases this write permission.
  299. * This is used for regular files.
  300. * We cannot support write (and maybe mmap read-write shared) accesses and
  301. * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
  302. * can have the following values:
  303. * 0: no writers, no VM_DENYWRITE mappings
  304. * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
  305. * > 0: (i_writecount) users are writing to the file.
  306. *
  307. * Normally we operate on that counter with atomic_{inc,dec} and it's safe
  308. * except for the cases where we don't hold i_writecount yet. Then we need to
  309. * use {get,deny}_write_access() - these functions check the sign and refuse
  310. * to do the change if sign is wrong. Exclusion between them is provided by
  311. * the inode->i_lock spinlock.
  312. */
  313. int get_write_access(struct inode * inode)
  314. {
  315. spin_lock(&inode->i_lock);
  316. if (atomic_read(&inode->i_writecount) < 0) {
  317. spin_unlock(&inode->i_lock);
  318. return -ETXTBSY;
  319. }
  320. atomic_inc(&inode->i_writecount);
  321. spin_unlock(&inode->i_lock);
  322. return 0;
  323. }
  324. int deny_write_access(struct file * file)
  325. {
  326. struct inode *inode = file->f_path.dentry->d_inode;
  327. spin_lock(&inode->i_lock);
  328. if (atomic_read(&inode->i_writecount) > 0) {
  329. spin_unlock(&inode->i_lock);
  330. return -ETXTBSY;
  331. }
  332. atomic_dec(&inode->i_writecount);
  333. spin_unlock(&inode->i_lock);
  334. return 0;
  335. }
  336. /**
  337. * path_get - get a reference to a path
  338. * @path: path to get the reference to
  339. *
  340. * Given a path increment the reference count to the dentry and the vfsmount.
  341. */
  342. void path_get(struct path *path)
  343. {
  344. mntget(path->mnt);
  345. dget(path->dentry);
  346. }
  347. EXPORT_SYMBOL(path_get);
  348. /**
  349. * path_put - put a reference to a path
  350. * @path: path to put the reference to
  351. *
  352. * Given a path decrement the reference count to the dentry and the vfsmount.
  353. */
  354. void path_put(struct path *path)
  355. {
  356. dput(path->dentry);
  357. mntput(path->mnt);
  358. }
  359. EXPORT_SYMBOL(path_put);
  360. /**
  361. * release_open_intent - free up open intent resources
  362. * @nd: pointer to nameidata
  363. */
  364. void release_open_intent(struct nameidata *nd)
  365. {
  366. if (nd->intent.open.file->f_path.dentry == NULL)
  367. put_filp(nd->intent.open.file);
  368. else
  369. fput(nd->intent.open.file);
  370. }
  371. static inline struct dentry *
  372. do_revalidate(struct dentry *dentry, struct nameidata *nd)
  373. {
  374. int status = dentry->d_op->d_revalidate(dentry, nd);
  375. if (unlikely(status <= 0)) {
  376. /*
  377. * The dentry failed validation.
  378. * If d_revalidate returned 0 attempt to invalidate
  379. * the dentry otherwise d_revalidate is asking us
  380. * to return a fail status.
  381. */
  382. if (!status) {
  383. if (!d_invalidate(dentry)) {
  384. dput(dentry);
  385. dentry = NULL;
  386. }
  387. } else {
  388. dput(dentry);
  389. dentry = ERR_PTR(status);
  390. }
  391. }
  392. return dentry;
  393. }
  394. /*
  395. * Internal lookup() using the new generic dcache.
  396. * SMP-safe
  397. */
  398. static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
  399. {
  400. struct dentry * dentry = __d_lookup(parent, name);
  401. /* lockess __d_lookup may fail due to concurrent d_move()
  402. * in some unrelated directory, so try with d_lookup
  403. */
  404. if (!dentry)
  405. dentry = d_lookup(parent, name);
  406. if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
  407. dentry = do_revalidate(dentry, nd);
  408. return dentry;
  409. }
  410. /*
  411. * Short-cut version of permission(), for calling by
  412. * path_walk(), when dcache lock is held. Combines parts
  413. * of permission() and generic_permission(), and tests ONLY for
  414. * MAY_EXEC permission.
  415. *
  416. * If appropriate, check DAC only. If not appropriate, or
  417. * short-cut DAC fails, then call permission() to do more
  418. * complete permission check.
  419. */
  420. static int exec_permission_lite(struct inode *inode,
  421. struct nameidata *nd)
  422. {
  423. umode_t mode = inode->i_mode;
  424. if (inode->i_op && inode->i_op->permission)
  425. return -EAGAIN;
  426. if (current->fsuid == inode->i_uid)
  427. mode >>= 6;
  428. else if (in_group_p(inode->i_gid))
  429. mode >>= 3;
  430. if (mode & MAY_EXEC)
  431. goto ok;
  432. if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
  433. goto ok;
  434. if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
  435. goto ok;
  436. if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
  437. goto ok;
  438. return -EACCES;
  439. ok:
  440. return security_inode_permission(inode, MAY_EXEC, nd);
  441. }
  442. /*
  443. * This is called when everything else fails, and we actually have
  444. * to go to the low-level filesystem to find out what we should do..
  445. *
  446. * We get the directory semaphore, and after getting that we also
  447. * make sure that nobody added the entry to the dcache in the meantime..
  448. * SMP-safe
  449. */
  450. static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
  451. {
  452. struct dentry * result;
  453. struct inode *dir = parent->d_inode;
  454. mutex_lock(&dir->i_mutex);
  455. /*
  456. * First re-do the cached lookup just in case it was created
  457. * while we waited for the directory semaphore..
  458. *
  459. * FIXME! This could use version numbering or similar to
  460. * avoid unnecessary cache lookups.
  461. *
  462. * The "dcache_lock" is purely to protect the RCU list walker
  463. * from concurrent renames at this point (we mustn't get false
  464. * negatives from the RCU list walk here, unlike the optimistic
  465. * fast walk).
  466. *
  467. * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
  468. */
  469. result = d_lookup(parent, name);
  470. if (!result) {
  471. struct dentry *dentry;
  472. /* Don't create child dentry for a dead directory. */
  473. result = ERR_PTR(-ENOENT);
  474. if (IS_DEADDIR(dir))
  475. goto out_unlock;
  476. dentry = d_alloc(parent, name);
  477. result = ERR_PTR(-ENOMEM);
  478. if (dentry) {
  479. result = dir->i_op->lookup(dir, dentry, nd);
  480. if (result)
  481. dput(dentry);
  482. else
  483. result = dentry;
  484. }
  485. out_unlock:
  486. mutex_unlock(&dir->i_mutex);
  487. return result;
  488. }
  489. /*
  490. * Uhhuh! Nasty case: the cache was re-populated while
  491. * we waited on the semaphore. Need to revalidate.
  492. */
  493. mutex_unlock(&dir->i_mutex);
  494. if (result->d_op && result->d_op->d_revalidate) {
  495. result = do_revalidate(result, nd);
  496. if (!result)
  497. result = ERR_PTR(-ENOENT);
  498. }
  499. return result;
  500. }
  501. static int __emul_lookup_dentry(const char *, struct nameidata *);
  502. /* SMP-safe */
  503. static __always_inline int
  504. walk_init_root(const char *name, struct nameidata *nd)
  505. {
  506. struct fs_struct *fs = current->fs;
  507. read_lock(&fs->lock);
  508. if (fs->altroot.dentry && !(nd->flags & LOOKUP_NOALT)) {
  509. nd->path = fs->altroot;
  510. path_get(&fs->altroot);
  511. read_unlock(&fs->lock);
  512. if (__emul_lookup_dentry(name,nd))
  513. return 0;
  514. read_lock(&fs->lock);
  515. }
  516. nd->path = fs->root;
  517. path_get(&fs->root);
  518. read_unlock(&fs->lock);
  519. return 1;
  520. }
  521. /*
  522. * Wrapper to retry pathname resolution whenever the underlying
  523. * file system returns an ESTALE.
  524. *
  525. * Retry the whole path once, forcing real lookup requests
  526. * instead of relying on the dcache.
  527. */
  528. static __always_inline int link_path_walk(const char *name, struct nameidata *nd)
  529. {
  530. struct path save = nd->path;
  531. int result;
  532. /* make sure the stuff we saved doesn't go away */
  533. path_get(&save);
  534. result = __link_path_walk(name, nd);
  535. if (result == -ESTALE) {
  536. /* nd->path had been dropped */
  537. nd->path = save;
  538. path_get(&nd->path);
  539. nd->flags |= LOOKUP_REVAL;
  540. result = __link_path_walk(name, nd);
  541. }
  542. path_put(&save);
  543. return result;
  544. }
  545. static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
  546. {
  547. int res = 0;
  548. char *name;
  549. if (IS_ERR(link))
  550. goto fail;
  551. if (*link == '/') {
  552. path_put(&nd->path);
  553. if (!walk_init_root(link, nd))
  554. /* weird __emul_prefix() stuff did it */
  555. goto out;
  556. }
  557. res = link_path_walk(link, nd);
  558. out:
  559. if (nd->depth || res || nd->last_type!=LAST_NORM)
  560. return res;
  561. /*
  562. * If it is an iterative symlinks resolution in open_namei() we
  563. * have to copy the last component. And all that crap because of
  564. * bloody create() on broken symlinks. Furrfu...
  565. */
  566. name = __getname();
  567. if (unlikely(!name)) {
  568. path_put(&nd->path);
  569. return -ENOMEM;
  570. }
  571. strcpy(name, nd->last.name);
  572. nd->last.name = name;
  573. return 0;
  574. fail:
  575. path_put(&nd->path);
  576. return PTR_ERR(link);
  577. }
  578. static void path_put_conditional(struct path *path, struct nameidata *nd)
  579. {
  580. dput(path->dentry);
  581. if (path->mnt != nd->path.mnt)
  582. mntput(path->mnt);
  583. }
  584. static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
  585. {
  586. dput(nd->path.dentry);
  587. if (nd->path.mnt != path->mnt)
  588. mntput(nd->path.mnt);
  589. nd->path.mnt = path->mnt;
  590. nd->path.dentry = path->dentry;
  591. }
  592. static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
  593. {
  594. int error;
  595. void *cookie;
  596. struct dentry *dentry = path->dentry;
  597. touch_atime(path->mnt, dentry);
  598. nd_set_link(nd, NULL);
  599. if (path->mnt != nd->path.mnt) {
  600. path_to_nameidata(path, nd);
  601. dget(dentry);
  602. }
  603. mntget(path->mnt);
  604. cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
  605. error = PTR_ERR(cookie);
  606. if (!IS_ERR(cookie)) {
  607. char *s = nd_get_link(nd);
  608. error = 0;
  609. if (s)
  610. error = __vfs_follow_link(nd, s);
  611. if (dentry->d_inode->i_op->put_link)
  612. dentry->d_inode->i_op->put_link(dentry, nd, cookie);
  613. }
  614. path_put(path);
  615. return error;
  616. }
  617. /*
  618. * This limits recursive symlink follows to 8, while
  619. * limiting consecutive symlinks to 40.
  620. *
  621. * Without that kind of total limit, nasty chains of consecutive
  622. * symlinks can cause almost arbitrarily long lookups.
  623. */
  624. static inline int do_follow_link(struct path *path, struct nameidata *nd)
  625. {
  626. int err = -ELOOP;
  627. if (current->link_count >= MAX_NESTED_LINKS)
  628. goto loop;
  629. if (current->total_link_count >= 40)
  630. goto loop;
  631. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  632. cond_resched();
  633. err = security_inode_follow_link(path->dentry, nd);
  634. if (err)
  635. goto loop;
  636. current->link_count++;
  637. current->total_link_count++;
  638. nd->depth++;
  639. err = __do_follow_link(path, nd);
  640. current->link_count--;
  641. nd->depth--;
  642. return err;
  643. loop:
  644. path_put_conditional(path, nd);
  645. path_put(&nd->path);
  646. return err;
  647. }
  648. int follow_up(struct vfsmount **mnt, struct dentry **dentry)
  649. {
  650. struct vfsmount *parent;
  651. struct dentry *mountpoint;
  652. spin_lock(&vfsmount_lock);
  653. parent=(*mnt)->mnt_parent;
  654. if (parent == *mnt) {
  655. spin_unlock(&vfsmount_lock);
  656. return 0;
  657. }
  658. mntget(parent);
  659. mountpoint=dget((*mnt)->mnt_mountpoint);
  660. spin_unlock(&vfsmount_lock);
  661. dput(*dentry);
  662. *dentry = mountpoint;
  663. mntput(*mnt);
  664. *mnt = parent;
  665. return 1;
  666. }
  667. /* no need for dcache_lock, as serialization is taken care in
  668. * namespace.c
  669. */
  670. static int __follow_mount(struct path *path)
  671. {
  672. int res = 0;
  673. while (d_mountpoint(path->dentry)) {
  674. struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
  675. if (!mounted)
  676. break;
  677. dput(path->dentry);
  678. if (res)
  679. mntput(path->mnt);
  680. path->mnt = mounted;
  681. path->dentry = dget(mounted->mnt_root);
  682. res = 1;
  683. }
  684. return res;
  685. }
  686. static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
  687. {
  688. while (d_mountpoint(*dentry)) {
  689. struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
  690. if (!mounted)
  691. break;
  692. dput(*dentry);
  693. mntput(*mnt);
  694. *mnt = mounted;
  695. *dentry = dget(mounted->mnt_root);
  696. }
  697. }
  698. /* no need for dcache_lock, as serialization is taken care in
  699. * namespace.c
  700. */
  701. int follow_down(struct vfsmount **mnt, struct dentry **dentry)
  702. {
  703. struct vfsmount *mounted;
  704. mounted = lookup_mnt(*mnt, *dentry);
  705. if (mounted) {
  706. dput(*dentry);
  707. mntput(*mnt);
  708. *mnt = mounted;
  709. *dentry = dget(mounted->mnt_root);
  710. return 1;
  711. }
  712. return 0;
  713. }
  714. static __always_inline void follow_dotdot(struct nameidata *nd)
  715. {
  716. struct fs_struct *fs = current->fs;
  717. while(1) {
  718. struct vfsmount *parent;
  719. struct dentry *old = nd->path.dentry;
  720. read_lock(&fs->lock);
  721. if (nd->path.dentry == fs->root.dentry &&
  722. nd->path.mnt == fs->root.mnt) {
  723. read_unlock(&fs->lock);
  724. break;
  725. }
  726. read_unlock(&fs->lock);
  727. spin_lock(&dcache_lock);
  728. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  729. nd->path.dentry = dget(nd->path.dentry->d_parent);
  730. spin_unlock(&dcache_lock);
  731. dput(old);
  732. break;
  733. }
  734. spin_unlock(&dcache_lock);
  735. spin_lock(&vfsmount_lock);
  736. parent = nd->path.mnt->mnt_parent;
  737. if (parent == nd->path.mnt) {
  738. spin_unlock(&vfsmount_lock);
  739. break;
  740. }
  741. mntget(parent);
  742. nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint);
  743. spin_unlock(&vfsmount_lock);
  744. dput(old);
  745. mntput(nd->path.mnt);
  746. nd->path.mnt = parent;
  747. }
  748. follow_mount(&nd->path.mnt, &nd->path.dentry);
  749. }
  750. /*
  751. * It's more convoluted than I'd like it to be, but... it's still fairly
  752. * small and for now I'd prefer to have fast path as straight as possible.
  753. * It _is_ time-critical.
  754. */
  755. static int do_lookup(struct nameidata *nd, struct qstr *name,
  756. struct path *path)
  757. {
  758. struct vfsmount *mnt = nd->path.mnt;
  759. struct dentry *dentry = __d_lookup(nd->path.dentry, name);
  760. if (!dentry)
  761. goto need_lookup;
  762. if (dentry->d_op && dentry->d_op->d_revalidate)
  763. goto need_revalidate;
  764. done:
  765. path->mnt = mnt;
  766. path->dentry = dentry;
  767. __follow_mount(path);
  768. return 0;
  769. need_lookup:
  770. dentry = real_lookup(nd->path.dentry, name, nd);
  771. if (IS_ERR(dentry))
  772. goto fail;
  773. goto done;
  774. need_revalidate:
  775. dentry = do_revalidate(dentry, nd);
  776. if (!dentry)
  777. goto need_lookup;
  778. if (IS_ERR(dentry))
  779. goto fail;
  780. goto done;
  781. fail:
  782. return PTR_ERR(dentry);
  783. }
  784. /*
  785. * Name resolution.
  786. * This is the basic name resolution function, turning a pathname into
  787. * the final dentry. We expect 'base' to be positive and a directory.
  788. *
  789. * Returns 0 and nd will have valid dentry and mnt on success.
  790. * Returns error and drops reference to input namei data on failure.
  791. */
  792. static int __link_path_walk(const char *name, struct nameidata *nd)
  793. {
  794. struct path next;
  795. struct inode *inode;
  796. int err;
  797. unsigned int lookup_flags = nd->flags;
  798. while (*name=='/')
  799. name++;
  800. if (!*name)
  801. goto return_reval;
  802. inode = nd->path.dentry->d_inode;
  803. if (nd->depth)
  804. lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
  805. /* At this point we know we have a real path component. */
  806. for(;;) {
  807. unsigned long hash;
  808. struct qstr this;
  809. unsigned int c;
  810. nd->flags |= LOOKUP_CONTINUE;
  811. err = exec_permission_lite(inode, nd);
  812. if (err == -EAGAIN)
  813. err = vfs_permission(nd, MAY_EXEC);
  814. if (err)
  815. break;
  816. this.name = name;
  817. c = *(const unsigned char *)name;
  818. hash = init_name_hash();
  819. do {
  820. name++;
  821. hash = partial_name_hash(c, hash);
  822. c = *(const unsigned char *)name;
  823. } while (c && (c != '/'));
  824. this.len = name - (const char *) this.name;
  825. this.hash = end_name_hash(hash);
  826. /* remove trailing slashes? */
  827. if (!c)
  828. goto last_component;
  829. while (*++name == '/');
  830. if (!*name)
  831. goto last_with_slashes;
  832. /*
  833. * "." and ".." are special - ".." especially so because it has
  834. * to be able to know about the current root directory and
  835. * parent relationships.
  836. */
  837. if (this.name[0] == '.') switch (this.len) {
  838. default:
  839. break;
  840. case 2:
  841. if (this.name[1] != '.')
  842. break;
  843. follow_dotdot(nd);
  844. inode = nd->path.dentry->d_inode;
  845. /* fallthrough */
  846. case 1:
  847. continue;
  848. }
  849. /*
  850. * See if the low-level filesystem might want
  851. * to use its own hash..
  852. */
  853. if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
  854. err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
  855. &this);
  856. if (err < 0)
  857. break;
  858. }
  859. /* This does the actual lookups.. */
  860. err = do_lookup(nd, &this, &next);
  861. if (err)
  862. break;
  863. err = -ENOENT;
  864. inode = next.dentry->d_inode;
  865. if (!inode)
  866. goto out_dput;
  867. err = -ENOTDIR;
  868. if (!inode->i_op)
  869. goto out_dput;
  870. if (inode->i_op->follow_link) {
  871. err = do_follow_link(&next, nd);
  872. if (err)
  873. goto return_err;
  874. err = -ENOENT;
  875. inode = nd->path.dentry->d_inode;
  876. if (!inode)
  877. break;
  878. err = -ENOTDIR;
  879. if (!inode->i_op)
  880. break;
  881. } else
  882. path_to_nameidata(&next, nd);
  883. err = -ENOTDIR;
  884. if (!inode->i_op->lookup)
  885. break;
  886. continue;
  887. /* here ends the main loop */
  888. last_with_slashes:
  889. lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  890. last_component:
  891. /* Clear LOOKUP_CONTINUE iff it was previously unset */
  892. nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
  893. if (lookup_flags & LOOKUP_PARENT)
  894. goto lookup_parent;
  895. if (this.name[0] == '.') switch (this.len) {
  896. default:
  897. break;
  898. case 2:
  899. if (this.name[1] != '.')
  900. break;
  901. follow_dotdot(nd);
  902. inode = nd->path.dentry->d_inode;
  903. /* fallthrough */
  904. case 1:
  905. goto return_reval;
  906. }
  907. if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
  908. err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
  909. &this);
  910. if (err < 0)
  911. break;
  912. }
  913. err = do_lookup(nd, &this, &next);
  914. if (err)
  915. break;
  916. inode = next.dentry->d_inode;
  917. if ((lookup_flags & LOOKUP_FOLLOW)
  918. && inode && inode->i_op && inode->i_op->follow_link) {
  919. err = do_follow_link(&next, nd);
  920. if (err)
  921. goto return_err;
  922. inode = nd->path.dentry->d_inode;
  923. } else
  924. path_to_nameidata(&next, nd);
  925. err = -ENOENT;
  926. if (!inode)
  927. break;
  928. if (lookup_flags & LOOKUP_DIRECTORY) {
  929. err = -ENOTDIR;
  930. if (!inode->i_op || !inode->i_op->lookup)
  931. break;
  932. }
  933. goto return_base;
  934. lookup_parent:
  935. nd->last = this;
  936. nd->last_type = LAST_NORM;
  937. if (this.name[0] != '.')
  938. goto return_base;
  939. if (this.len == 1)
  940. nd->last_type = LAST_DOT;
  941. else if (this.len == 2 && this.name[1] == '.')
  942. nd->last_type = LAST_DOTDOT;
  943. else
  944. goto return_base;
  945. return_reval:
  946. /*
  947. * We bypassed the ordinary revalidation routines.
  948. * We may need to check the cached dentry for staleness.
  949. */
  950. if (nd->path.dentry && nd->path.dentry->d_sb &&
  951. (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
  952. err = -ESTALE;
  953. /* Note: we do not d_invalidate() */
  954. if (!nd->path.dentry->d_op->d_revalidate(
  955. nd->path.dentry, nd))
  956. break;
  957. }
  958. return_base:
  959. return 0;
  960. out_dput:
  961. path_put_conditional(&next, nd);
  962. break;
  963. }
  964. path_put(&nd->path);
  965. return_err:
  966. return err;
  967. }
  968. static int path_walk(const char *name, struct nameidata *nd)
  969. {
  970. current->total_link_count = 0;
  971. return link_path_walk(name, nd);
  972. }
  973. /*
  974. * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
  975. * everything is done. Returns 0 and drops input nd, if lookup failed;
  976. */
  977. static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
  978. {
  979. if (path_walk(name, nd))
  980. return 0; /* something went wrong... */
  981. if (!nd->path.dentry->d_inode ||
  982. S_ISDIR(nd->path.dentry->d_inode->i_mode)) {
  983. struct path old_path = nd->path;
  984. struct qstr last = nd->last;
  985. int last_type = nd->last_type;
  986. struct fs_struct *fs = current->fs;
  987. /*
  988. * NAME was not found in alternate root or it's a directory.
  989. * Try to find it in the normal root:
  990. */
  991. nd->last_type = LAST_ROOT;
  992. read_lock(&fs->lock);
  993. nd->path = fs->root;
  994. path_get(&fs->root);
  995. read_unlock(&fs->lock);
  996. if (path_walk(name, nd) == 0) {
  997. if (nd->path.dentry->d_inode) {
  998. path_put(&old_path);
  999. return 1;
  1000. }
  1001. path_put(&nd->path);
  1002. }
  1003. nd->path = old_path;
  1004. nd->last = last;
  1005. nd->last_type = last_type;
  1006. }
  1007. return 1;
  1008. }
  1009. void set_fs_altroot(void)
  1010. {
  1011. char *emul = __emul_prefix();
  1012. struct nameidata nd;
  1013. struct path path = {}, old_path;
  1014. int err;
  1015. struct fs_struct *fs = current->fs;
  1016. if (!emul)
  1017. goto set_it;
  1018. err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
  1019. if (!err)
  1020. path = nd.path;
  1021. set_it:
  1022. write_lock(&fs->lock);
  1023. old_path = fs->altroot;
  1024. fs->altroot = path;
  1025. write_unlock(&fs->lock);
  1026. if (old_path.dentry)
  1027. path_put(&old_path);
  1028. }
  1029. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1030. static int do_path_lookup(int dfd, const char *name,
  1031. unsigned int flags, struct nameidata *nd)
  1032. {
  1033. int retval = 0;
  1034. int fput_needed;
  1035. struct file *file;
  1036. struct fs_struct *fs = current->fs;
  1037. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1038. nd->flags = flags;
  1039. nd->depth = 0;
  1040. if (*name=='/') {
  1041. read_lock(&fs->lock);
  1042. if (fs->altroot.dentry && !(nd->flags & LOOKUP_NOALT)) {
  1043. nd->path = fs->altroot;
  1044. path_get(&fs->altroot);
  1045. read_unlock(&fs->lock);
  1046. if (__emul_lookup_dentry(name,nd))
  1047. goto out; /* found in altroot */
  1048. read_lock(&fs->lock);
  1049. }
  1050. nd->path = fs->root;
  1051. path_get(&fs->root);
  1052. read_unlock(&fs->lock);
  1053. } else if (dfd == AT_FDCWD) {
  1054. read_lock(&fs->lock);
  1055. nd->path = fs->pwd;
  1056. path_get(&fs->pwd);
  1057. read_unlock(&fs->lock);
  1058. } else {
  1059. struct dentry *dentry;
  1060. file = fget_light(dfd, &fput_needed);
  1061. retval = -EBADF;
  1062. if (!file)
  1063. goto out_fail;
  1064. dentry = file->f_path.dentry;
  1065. retval = -ENOTDIR;
  1066. if (!S_ISDIR(dentry->d_inode->i_mode))
  1067. goto fput_fail;
  1068. retval = file_permission(file, MAY_EXEC);
  1069. if (retval)
  1070. goto fput_fail;
  1071. nd->path = file->f_path;
  1072. path_get(&file->f_path);
  1073. fput_light(file, fput_needed);
  1074. }
  1075. retval = path_walk(name, nd);
  1076. out:
  1077. if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
  1078. nd->path.dentry->d_inode))
  1079. audit_inode(name, nd->path.dentry);
  1080. out_fail:
  1081. return retval;
  1082. fput_fail:
  1083. fput_light(file, fput_needed);
  1084. goto out_fail;
  1085. }
  1086. int path_lookup(const char *name, unsigned int flags,
  1087. struct nameidata *nd)
  1088. {
  1089. return do_path_lookup(AT_FDCWD, name, flags, nd);
  1090. }
  1091. /**
  1092. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  1093. * @dentry: pointer to dentry of the base directory
  1094. * @mnt: pointer to vfs mount of the base directory
  1095. * @name: pointer to file name
  1096. * @flags: lookup flags
  1097. * @nd: pointer to nameidata
  1098. */
  1099. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1100. const char *name, unsigned int flags,
  1101. struct nameidata *nd)
  1102. {
  1103. int retval;
  1104. /* same as do_path_lookup */
  1105. nd->last_type = LAST_ROOT;
  1106. nd->flags = flags;
  1107. nd->depth = 0;
  1108. nd->path.dentry = dentry;
  1109. nd->path.mnt = mnt;
  1110. path_get(&nd->path);
  1111. retval = path_walk(name, nd);
  1112. if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
  1113. nd->path.dentry->d_inode))
  1114. audit_inode(name, nd->path.dentry);
  1115. return retval;
  1116. }
  1117. static int __path_lookup_intent_open(int dfd, const char *name,
  1118. unsigned int lookup_flags, struct nameidata *nd,
  1119. int open_flags, int create_mode)
  1120. {
  1121. struct file *filp = get_empty_filp();
  1122. int err;
  1123. if (filp == NULL)
  1124. return -ENFILE;
  1125. nd->intent.open.file = filp;
  1126. nd->intent.open.flags = open_flags;
  1127. nd->intent.open.create_mode = create_mode;
  1128. err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
  1129. if (IS_ERR(nd->intent.open.file)) {
  1130. if (err == 0) {
  1131. err = PTR_ERR(nd->intent.open.file);
  1132. path_put(&nd->path);
  1133. }
  1134. } else if (err != 0)
  1135. release_open_intent(nd);
  1136. return err;
  1137. }
  1138. /**
  1139. * path_lookup_open - lookup a file path with open intent
  1140. * @dfd: the directory to use as base, or AT_FDCWD
  1141. * @name: pointer to file name
  1142. * @lookup_flags: lookup intent flags
  1143. * @nd: pointer to nameidata
  1144. * @open_flags: open intent flags
  1145. */
  1146. int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
  1147. struct nameidata *nd, int open_flags)
  1148. {
  1149. return __path_lookup_intent_open(dfd, name, lookup_flags, nd,
  1150. open_flags, 0);
  1151. }
  1152. /**
  1153. * path_lookup_create - lookup a file path with open + create intent
  1154. * @dfd: the directory to use as base, or AT_FDCWD
  1155. * @name: pointer to file name
  1156. * @lookup_flags: lookup intent flags
  1157. * @nd: pointer to nameidata
  1158. * @open_flags: open intent flags
  1159. * @create_mode: create intent flags
  1160. */
  1161. static int path_lookup_create(int dfd, const char *name,
  1162. unsigned int lookup_flags, struct nameidata *nd,
  1163. int open_flags, int create_mode)
  1164. {
  1165. return __path_lookup_intent_open(dfd, name, lookup_flags|LOOKUP_CREATE,
  1166. nd, open_flags, create_mode);
  1167. }
  1168. int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
  1169. struct nameidata *nd, int open_flags)
  1170. {
  1171. char *tmp = getname(name);
  1172. int err = PTR_ERR(tmp);
  1173. if (!IS_ERR(tmp)) {
  1174. err = __path_lookup_intent_open(AT_FDCWD, tmp, lookup_flags, nd, open_flags, 0);
  1175. putname(tmp);
  1176. }
  1177. return err;
  1178. }
  1179. static struct dentry *__lookup_hash(struct qstr *name,
  1180. struct dentry *base, struct nameidata *nd)
  1181. {
  1182. struct dentry *dentry;
  1183. struct inode *inode;
  1184. int err;
  1185. inode = base->d_inode;
  1186. /*
  1187. * See if the low-level filesystem might want
  1188. * to use its own hash..
  1189. */
  1190. if (base->d_op && base->d_op->d_hash) {
  1191. err = base->d_op->d_hash(base, name);
  1192. dentry = ERR_PTR(err);
  1193. if (err < 0)
  1194. goto out;
  1195. }
  1196. dentry = cached_lookup(base, name, nd);
  1197. if (!dentry) {
  1198. struct dentry *new;
  1199. /* Don't create child dentry for a dead directory. */
  1200. dentry = ERR_PTR(-ENOENT);
  1201. if (IS_DEADDIR(inode))
  1202. goto out;
  1203. new = d_alloc(base, name);
  1204. dentry = ERR_PTR(-ENOMEM);
  1205. if (!new)
  1206. goto out;
  1207. dentry = inode->i_op->lookup(inode, new, nd);
  1208. if (!dentry)
  1209. dentry = new;
  1210. else
  1211. dput(new);
  1212. }
  1213. out:
  1214. return dentry;
  1215. }
  1216. /*
  1217. * Restricted form of lookup. Doesn't follow links, single-component only,
  1218. * needs parent already locked. Doesn't follow mounts.
  1219. * SMP-safe.
  1220. */
  1221. static struct dentry *lookup_hash(struct nameidata *nd)
  1222. {
  1223. int err;
  1224. err = permission(nd->path.dentry->d_inode, MAY_EXEC, nd);
  1225. if (err)
  1226. return ERR_PTR(err);
  1227. return __lookup_hash(&nd->last, nd->path.dentry, nd);
  1228. }
  1229. static int __lookup_one_len(const char *name, struct qstr *this,
  1230. struct dentry *base, int len)
  1231. {
  1232. unsigned long hash;
  1233. unsigned int c;
  1234. this->name = name;
  1235. this->len = len;
  1236. if (!len)
  1237. return -EACCES;
  1238. hash = init_name_hash();
  1239. while (len--) {
  1240. c = *(const unsigned char *)name++;
  1241. if (c == '/' || c == '\0')
  1242. return -EACCES;
  1243. hash = partial_name_hash(c, hash);
  1244. }
  1245. this->hash = end_name_hash(hash);
  1246. return 0;
  1247. }
  1248. /**
  1249. * lookup_one_len - filesystem helper to lookup single pathname component
  1250. * @name: pathname component to lookup
  1251. * @base: base directory to lookup from
  1252. * @len: maximum length @len should be interpreted to
  1253. *
  1254. * Note that this routine is purely a helper for filesystem usage and should
  1255. * not be called by generic code. Also note that by using this function the
  1256. * nameidata argument is passed to the filesystem methods and a filesystem
  1257. * using this helper needs to be prepared for that.
  1258. */
  1259. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1260. {
  1261. int err;
  1262. struct qstr this;
  1263. err = __lookup_one_len(name, &this, base, len);
  1264. if (err)
  1265. return ERR_PTR(err);
  1266. err = permission(base->d_inode, MAY_EXEC, NULL);
  1267. if (err)
  1268. return ERR_PTR(err);
  1269. return __lookup_hash(&this, base, NULL);
  1270. }
  1271. /**
  1272. * lookup_one_noperm - bad hack for sysfs
  1273. * @name: pathname component to lookup
  1274. * @base: base directory to lookup from
  1275. *
  1276. * This is a variant of lookup_one_len that doesn't perform any permission
  1277. * checks. It's a horrible hack to work around the braindead sysfs
  1278. * architecture and should not be used anywhere else.
  1279. *
  1280. * DON'T USE THIS FUNCTION EVER, thanks.
  1281. */
  1282. struct dentry *lookup_one_noperm(const char *name, struct dentry *base)
  1283. {
  1284. int err;
  1285. struct qstr this;
  1286. err = __lookup_one_len(name, &this, base, strlen(name));
  1287. if (err)
  1288. return ERR_PTR(err);
  1289. return __lookup_hash(&this, base, NULL);
  1290. }
  1291. int __user_walk_fd(int dfd, const char __user *name, unsigned flags,
  1292. struct nameidata *nd)
  1293. {
  1294. char *tmp = getname(name);
  1295. int err = PTR_ERR(tmp);
  1296. if (!IS_ERR(tmp)) {
  1297. err = do_path_lookup(dfd, tmp, flags, nd);
  1298. putname(tmp);
  1299. }
  1300. return err;
  1301. }
  1302. int __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
  1303. {
  1304. return __user_walk_fd(AT_FDCWD, name, flags, nd);
  1305. }
  1306. /*
  1307. * It's inline, so penalty for filesystems that don't use sticky bit is
  1308. * minimal.
  1309. */
  1310. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1311. {
  1312. if (!(dir->i_mode & S_ISVTX))
  1313. return 0;
  1314. if (inode->i_uid == current->fsuid)
  1315. return 0;
  1316. if (dir->i_uid == current->fsuid)
  1317. return 0;
  1318. return !capable(CAP_FOWNER);
  1319. }
  1320. /*
  1321. * Check whether we can remove a link victim from directory dir, check
  1322. * whether the type of victim is right.
  1323. * 1. We can't do it if dir is read-only (done in permission())
  1324. * 2. We should have write and exec permissions on dir
  1325. * 3. We can't remove anything from append-only dir
  1326. * 4. We can't do anything with immutable dir (done in permission())
  1327. * 5. If the sticky bit on dir is set we should either
  1328. * a. be owner of dir, or
  1329. * b. be owner of victim, or
  1330. * c. have CAP_FOWNER capability
  1331. * 6. If the victim is append-only or immutable we can't do antyhing with
  1332. * links pointing to it.
  1333. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1334. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1335. * 9. We can't remove a root or mountpoint.
  1336. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1337. * nfs_async_unlink().
  1338. */
  1339. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1340. {
  1341. int error;
  1342. if (!victim->d_inode)
  1343. return -ENOENT;
  1344. BUG_ON(victim->d_parent->d_inode != dir);
  1345. audit_inode_child(victim->d_name.name, victim, dir);
  1346. error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
  1347. if (error)
  1348. return error;
  1349. if (IS_APPEND(dir))
  1350. return -EPERM;
  1351. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1352. IS_IMMUTABLE(victim->d_inode))
  1353. return -EPERM;
  1354. if (isdir) {
  1355. if (!S_ISDIR(victim->d_inode->i_mode))
  1356. return -ENOTDIR;
  1357. if (IS_ROOT(victim))
  1358. return -EBUSY;
  1359. } else if (S_ISDIR(victim->d_inode->i_mode))
  1360. return -EISDIR;
  1361. if (IS_DEADDIR(dir))
  1362. return -ENOENT;
  1363. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1364. return -EBUSY;
  1365. return 0;
  1366. }
  1367. /* Check whether we can create an object with dentry child in directory
  1368. * dir.
  1369. * 1. We can't do it if child already exists (open has special treatment for
  1370. * this case, but since we are inlined it's OK)
  1371. * 2. We can't do it if dir is read-only (done in permission())
  1372. * 3. We should have write and exec permissions on dir
  1373. * 4. We can't do it if dir is immutable (done in permission())
  1374. */
  1375. static inline int may_create(struct inode *dir, struct dentry *child,
  1376. struct nameidata *nd)
  1377. {
  1378. if (child->d_inode)
  1379. return -EEXIST;
  1380. if (IS_DEADDIR(dir))
  1381. return -ENOENT;
  1382. return permission(dir,MAY_WRITE | MAY_EXEC, nd);
  1383. }
  1384. /*
  1385. * O_DIRECTORY translates into forcing a directory lookup.
  1386. */
  1387. static inline int lookup_flags(unsigned int f)
  1388. {
  1389. unsigned long retval = LOOKUP_FOLLOW;
  1390. if (f & O_NOFOLLOW)
  1391. retval &= ~LOOKUP_FOLLOW;
  1392. if (f & O_DIRECTORY)
  1393. retval |= LOOKUP_DIRECTORY;
  1394. return retval;
  1395. }
  1396. /*
  1397. * p1 and p2 should be directories on the same fs.
  1398. */
  1399. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1400. {
  1401. struct dentry *p;
  1402. if (p1 == p2) {
  1403. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1404. return NULL;
  1405. }
  1406. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1407. for (p = p1; p->d_parent != p; p = p->d_parent) {
  1408. if (p->d_parent == p2) {
  1409. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1410. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1411. return p;
  1412. }
  1413. }
  1414. for (p = p2; p->d_parent != p; p = p->d_parent) {
  1415. if (p->d_parent == p1) {
  1416. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1417. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1418. return p;
  1419. }
  1420. }
  1421. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1422. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1423. return NULL;
  1424. }
  1425. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1426. {
  1427. mutex_unlock(&p1->d_inode->i_mutex);
  1428. if (p1 != p2) {
  1429. mutex_unlock(&p2->d_inode->i_mutex);
  1430. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1431. }
  1432. }
  1433. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1434. struct nameidata *nd)
  1435. {
  1436. int error = may_create(dir, dentry, nd);
  1437. if (error)
  1438. return error;
  1439. if (!dir->i_op || !dir->i_op->create)
  1440. return -EACCES; /* shouldn't it be ENOSYS? */
  1441. mode &= S_IALLUGO;
  1442. mode |= S_IFREG;
  1443. error = security_inode_create(dir, dentry, mode);
  1444. if (error)
  1445. return error;
  1446. DQUOT_INIT(dir);
  1447. error = dir->i_op->create(dir, dentry, mode, nd);
  1448. if (!error)
  1449. fsnotify_create(dir, dentry);
  1450. return error;
  1451. }
  1452. int may_open(struct nameidata *nd, int acc_mode, int flag)
  1453. {
  1454. struct dentry *dentry = nd->path.dentry;
  1455. struct inode *inode = dentry->d_inode;
  1456. int error;
  1457. if (!inode)
  1458. return -ENOENT;
  1459. if (S_ISLNK(inode->i_mode))
  1460. return -ELOOP;
  1461. if (S_ISDIR(inode->i_mode) && (acc_mode & MAY_WRITE))
  1462. return -EISDIR;
  1463. /*
  1464. * FIFO's, sockets and device files are special: they don't
  1465. * actually live on the filesystem itself, and as such you
  1466. * can write to them even if the filesystem is read-only.
  1467. */
  1468. if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  1469. flag &= ~O_TRUNC;
  1470. } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
  1471. if (nd->path.mnt->mnt_flags & MNT_NODEV)
  1472. return -EACCES;
  1473. flag &= ~O_TRUNC;
  1474. }
  1475. error = vfs_permission(nd, acc_mode);
  1476. if (error)
  1477. return error;
  1478. /*
  1479. * An append-only file must be opened in append mode for writing.
  1480. */
  1481. if (IS_APPEND(inode)) {
  1482. if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
  1483. return -EPERM;
  1484. if (flag & O_TRUNC)
  1485. return -EPERM;
  1486. }
  1487. /* O_NOATIME can only be set by the owner or superuser */
  1488. if (flag & O_NOATIME)
  1489. if (!is_owner_or_cap(inode))
  1490. return -EPERM;
  1491. /*
  1492. * Ensure there are no outstanding leases on the file.
  1493. */
  1494. error = break_lease(inode, flag);
  1495. if (error)
  1496. return error;
  1497. if (flag & O_TRUNC) {
  1498. error = get_write_access(inode);
  1499. if (error)
  1500. return error;
  1501. /*
  1502. * Refuse to truncate files with mandatory locks held on them.
  1503. */
  1504. error = locks_verify_locked(inode);
  1505. if (!error) {
  1506. DQUOT_INIT(inode);
  1507. error = do_truncate(dentry, 0,
  1508. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1509. NULL);
  1510. }
  1511. put_write_access(inode);
  1512. if (error)
  1513. return error;
  1514. } else
  1515. if (flag & FMODE_WRITE)
  1516. DQUOT_INIT(inode);
  1517. return 0;
  1518. }
  1519. /*
  1520. * Be careful about ever adding any more callers of this
  1521. * function. Its flags must be in the namei format, not
  1522. * what get passed to sys_open().
  1523. */
  1524. static int __open_namei_create(struct nameidata *nd, struct path *path,
  1525. int flag, int mode)
  1526. {
  1527. int error;
  1528. struct dentry *dir = nd->path.dentry;
  1529. if (!IS_POSIXACL(dir->d_inode))
  1530. mode &= ~current->fs->umask;
  1531. error = vfs_create(dir->d_inode, path->dentry, mode, nd);
  1532. mutex_unlock(&dir->d_inode->i_mutex);
  1533. dput(nd->path.dentry);
  1534. nd->path.dentry = path->dentry;
  1535. if (error)
  1536. return error;
  1537. /* Don't check for write permission, don't truncate */
  1538. return may_open(nd, 0, flag & ~O_TRUNC);
  1539. }
  1540. /*
  1541. * Note that while the flag value (low two bits) for sys_open means:
  1542. * 00 - read-only
  1543. * 01 - write-only
  1544. * 10 - read-write
  1545. * 11 - special
  1546. * it is changed into
  1547. * 00 - no permissions needed
  1548. * 01 - read-permission
  1549. * 10 - write-permission
  1550. * 11 - read-write
  1551. * for the internal routines (ie open_namei()/follow_link() etc)
  1552. * This is more logical, and also allows the 00 "no perm needed"
  1553. * to be used for symlinks (where the permissions are checked
  1554. * later).
  1555. *
  1556. */
  1557. static inline int open_to_namei_flags(int flag)
  1558. {
  1559. if ((flag+1) & O_ACCMODE)
  1560. flag++;
  1561. return flag;
  1562. }
  1563. static int open_will_write_to_fs(int flag, struct inode *inode)
  1564. {
  1565. /*
  1566. * We'll never write to the fs underlying
  1567. * a device file.
  1568. */
  1569. if (special_file(inode->i_mode))
  1570. return 0;
  1571. return (flag & O_TRUNC);
  1572. }
  1573. /*
  1574. * Note that the low bits of the passed in "open_flag"
  1575. * are not the same as in the local variable "flag". See
  1576. * open_to_namei_flags() for more details.
  1577. */
  1578. struct file *do_filp_open(int dfd, const char *pathname,
  1579. int open_flag, int mode)
  1580. {
  1581. struct file *filp;
  1582. struct nameidata nd;
  1583. int acc_mode, error;
  1584. struct path path;
  1585. struct dentry *dir;
  1586. int count = 0;
  1587. int will_write;
  1588. int flag = open_to_namei_flags(open_flag);
  1589. acc_mode = ACC_MODE(flag);
  1590. /* O_TRUNC implies we need access checks for write permissions */
  1591. if (flag & O_TRUNC)
  1592. acc_mode |= MAY_WRITE;
  1593. /* Allow the LSM permission hook to distinguish append
  1594. access from general write access. */
  1595. if (flag & O_APPEND)
  1596. acc_mode |= MAY_APPEND;
  1597. /*
  1598. * The simplest case - just a plain lookup.
  1599. */
  1600. if (!(flag & O_CREAT)) {
  1601. error = path_lookup_open(dfd, pathname, lookup_flags(flag),
  1602. &nd, flag);
  1603. if (error)
  1604. return ERR_PTR(error);
  1605. goto ok;
  1606. }
  1607. /*
  1608. * Create - we need to know the parent.
  1609. */
  1610. error = path_lookup_create(dfd, pathname, LOOKUP_PARENT,
  1611. &nd, flag, mode);
  1612. if (error)
  1613. return ERR_PTR(error);
  1614. /*
  1615. * We have the parent and last component. First of all, check
  1616. * that we are not asked to creat(2) an obvious directory - that
  1617. * will not do.
  1618. */
  1619. error = -EISDIR;
  1620. if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len])
  1621. goto exit;
  1622. dir = nd.path.dentry;
  1623. nd.flags &= ~LOOKUP_PARENT;
  1624. mutex_lock(&dir->d_inode->i_mutex);
  1625. path.dentry = lookup_hash(&nd);
  1626. path.mnt = nd.path.mnt;
  1627. do_last:
  1628. error = PTR_ERR(path.dentry);
  1629. if (IS_ERR(path.dentry)) {
  1630. mutex_unlock(&dir->d_inode->i_mutex);
  1631. goto exit;
  1632. }
  1633. if (IS_ERR(nd.intent.open.file)) {
  1634. error = PTR_ERR(nd.intent.open.file);
  1635. goto exit_mutex_unlock;
  1636. }
  1637. /* Negative dentry, just create the file */
  1638. if (!path.dentry->d_inode) {
  1639. /*
  1640. * This write is needed to ensure that a
  1641. * ro->rw transition does not occur between
  1642. * the time when the file is created and when
  1643. * a permanent write count is taken through
  1644. * the 'struct file' in nameidata_to_filp().
  1645. */
  1646. error = mnt_want_write(nd.path.mnt);
  1647. if (error)
  1648. goto exit_mutex_unlock;
  1649. error = __open_namei_create(&nd, &path, flag, mode);
  1650. if (error) {
  1651. mnt_drop_write(nd.path.mnt);
  1652. goto exit;
  1653. }
  1654. filp = nameidata_to_filp(&nd, open_flag);
  1655. mnt_drop_write(nd.path.mnt);
  1656. return filp;
  1657. }
  1658. /*
  1659. * It already exists.
  1660. */
  1661. mutex_unlock(&dir->d_inode->i_mutex);
  1662. audit_inode(pathname, path.dentry);
  1663. error = -EEXIST;
  1664. if (flag & O_EXCL)
  1665. goto exit_dput;
  1666. if (__follow_mount(&path)) {
  1667. error = -ELOOP;
  1668. if (flag & O_NOFOLLOW)
  1669. goto exit_dput;
  1670. }
  1671. error = -ENOENT;
  1672. if (!path.dentry->d_inode)
  1673. goto exit_dput;
  1674. if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
  1675. goto do_link;
  1676. path_to_nameidata(&path, &nd);
  1677. error = -EISDIR;
  1678. if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
  1679. goto exit;
  1680. ok:
  1681. /*
  1682. * Consider:
  1683. * 1. may_open() truncates a file
  1684. * 2. a rw->ro mount transition occurs
  1685. * 3. nameidata_to_filp() fails due to
  1686. * the ro mount.
  1687. * That would be inconsistent, and should
  1688. * be avoided. Taking this mnt write here
  1689. * ensures that (2) can not occur.
  1690. */
  1691. will_write = open_will_write_to_fs(flag, nd.path.dentry->d_inode);
  1692. if (will_write) {
  1693. error = mnt_want_write(nd.path.mnt);
  1694. if (error)
  1695. goto exit;
  1696. }
  1697. error = may_open(&nd, acc_mode, flag);
  1698. if (error) {
  1699. if (will_write)
  1700. mnt_drop_write(nd.path.mnt);
  1701. goto exit;
  1702. }
  1703. filp = nameidata_to_filp(&nd, open_flag);
  1704. /*
  1705. * It is now safe to drop the mnt write
  1706. * because the filp has had a write taken
  1707. * on its behalf.
  1708. */
  1709. if (will_write)
  1710. mnt_drop_write(nd.path.mnt);
  1711. return filp;
  1712. exit_mutex_unlock:
  1713. mutex_unlock(&dir->d_inode->i_mutex);
  1714. exit_dput:
  1715. path_put_conditional(&path, &nd);
  1716. exit:
  1717. if (!IS_ERR(nd.intent.open.file))
  1718. release_open_intent(&nd);
  1719. path_put(&nd.path);
  1720. return ERR_PTR(error);
  1721. do_link:
  1722. error = -ELOOP;
  1723. if (flag & O_NOFOLLOW)
  1724. goto exit_dput;
  1725. /*
  1726. * This is subtle. Instead of calling do_follow_link() we do the
  1727. * thing by hands. The reason is that this way we have zero link_count
  1728. * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
  1729. * After that we have the parent and last component, i.e.
  1730. * we are in the same situation as after the first path_walk().
  1731. * Well, almost - if the last component is normal we get its copy
  1732. * stored in nd->last.name and we will have to putname() it when we
  1733. * are done. Procfs-like symlinks just set LAST_BIND.
  1734. */
  1735. nd.flags |= LOOKUP_PARENT;
  1736. error = security_inode_follow_link(path.dentry, &nd);
  1737. if (error)
  1738. goto exit_dput;
  1739. error = __do_follow_link(&path, &nd);
  1740. if (error) {
  1741. /* Does someone understand code flow here? Or it is only
  1742. * me so stupid? Anathema to whoever designed this non-sense
  1743. * with "intent.open".
  1744. */
  1745. release_open_intent(&nd);
  1746. return ERR_PTR(error);
  1747. }
  1748. nd.flags &= ~LOOKUP_PARENT;
  1749. if (nd.last_type == LAST_BIND)
  1750. goto ok;
  1751. error = -EISDIR;
  1752. if (nd.last_type != LAST_NORM)
  1753. goto exit;
  1754. if (nd.last.name[nd.last.len]) {
  1755. __putname(nd.last.name);
  1756. goto exit;
  1757. }
  1758. error = -ELOOP;
  1759. if (count++==32) {
  1760. __putname(nd.last.name);
  1761. goto exit;
  1762. }
  1763. dir = nd.path.dentry;
  1764. mutex_lock(&dir->d_inode->i_mutex);
  1765. path.dentry = lookup_hash(&nd);
  1766. path.mnt = nd.path.mnt;
  1767. __putname(nd.last.name);
  1768. goto do_last;
  1769. }
  1770. /**
  1771. * filp_open - open file and return file pointer
  1772. *
  1773. * @filename: path to open
  1774. * @flags: open flags as per the open(2) second argument
  1775. * @mode: mode for the new file if O_CREAT is set, else ignored
  1776. *
  1777. * This is the helper to open a file from kernelspace if you really
  1778. * have to. But in generally you should not do this, so please move
  1779. * along, nothing to see here..
  1780. */
  1781. struct file *filp_open(const char *filename, int flags, int mode)
  1782. {
  1783. return do_filp_open(AT_FDCWD, filename, flags, mode);
  1784. }
  1785. EXPORT_SYMBOL(filp_open);
  1786. /**
  1787. * lookup_create - lookup a dentry, creating it if it doesn't exist
  1788. * @nd: nameidata info
  1789. * @is_dir: directory flag
  1790. *
  1791. * Simple function to lookup and return a dentry and create it
  1792. * if it doesn't exist. Is SMP-safe.
  1793. *
  1794. * Returns with nd->path.dentry->d_inode->i_mutex locked.
  1795. */
  1796. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  1797. {
  1798. struct dentry *dentry = ERR_PTR(-EEXIST);
  1799. mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1800. /*
  1801. * Yucky last component or no last component at all?
  1802. * (foo/., foo/.., /////)
  1803. */
  1804. if (nd->last_type != LAST_NORM)
  1805. goto fail;
  1806. nd->flags &= ~LOOKUP_PARENT;
  1807. nd->flags |= LOOKUP_CREATE;
  1808. nd->intent.open.flags = O_EXCL;
  1809. /*
  1810. * Do the final lookup.
  1811. */
  1812. dentry = lookup_hash(nd);
  1813. if (IS_ERR(dentry))
  1814. goto fail;
  1815. if (dentry->d_inode)
  1816. goto eexist;
  1817. /*
  1818. * Special case - lookup gave negative, but... we had foo/bar/
  1819. * From the vfs_mknod() POV we just have a negative dentry -
  1820. * all is fine. Let's be bastards - you had / on the end, you've
  1821. * been asking for (non-existent) directory. -ENOENT for you.
  1822. */
  1823. if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
  1824. dput(dentry);
  1825. dentry = ERR_PTR(-ENOENT);
  1826. }
  1827. return dentry;
  1828. eexist:
  1829. dput(dentry);
  1830. dentry = ERR_PTR(-EEXIST);
  1831. fail:
  1832. return dentry;
  1833. }
  1834. EXPORT_SYMBOL_GPL(lookup_create);
  1835. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1836. {
  1837. int error = may_create(dir, dentry, NULL);
  1838. if (error)
  1839. return error;
  1840. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  1841. return -EPERM;
  1842. if (!dir->i_op || !dir->i_op->mknod)
  1843. return -EPERM;
  1844. error = devcgroup_inode_mknod(mode, dev);
  1845. if (error)
  1846. return error;
  1847. error = security_inode_mknod(dir, dentry, mode, dev);
  1848. if (error)
  1849. return error;
  1850. DQUOT_INIT(dir);
  1851. error = dir->i_op->mknod(dir, dentry, mode, dev);
  1852. if (!error)
  1853. fsnotify_create(dir, dentry);
  1854. return error;
  1855. }
  1856. static int may_mknod(mode_t mode)
  1857. {
  1858. switch (mode & S_IFMT) {
  1859. case S_IFREG:
  1860. case S_IFCHR:
  1861. case S_IFBLK:
  1862. case S_IFIFO:
  1863. case S_IFSOCK:
  1864. case 0: /* zero mode translates to S_IFREG */
  1865. return 0;
  1866. case S_IFDIR:
  1867. return -EPERM;
  1868. default:
  1869. return -EINVAL;
  1870. }
  1871. }
  1872. asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
  1873. unsigned dev)
  1874. {
  1875. int error = 0;
  1876. char * tmp;
  1877. struct dentry * dentry;
  1878. struct nameidata nd;
  1879. if (S_ISDIR(mode))
  1880. return -EPERM;
  1881. tmp = getname(filename);
  1882. if (IS_ERR(tmp))
  1883. return PTR_ERR(tmp);
  1884. error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
  1885. if (error)
  1886. goto out;
  1887. dentry = lookup_create(&nd, 0);
  1888. if (IS_ERR(dentry)) {
  1889. error = PTR_ERR(dentry);
  1890. goto out_unlock;
  1891. }
  1892. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  1893. mode &= ~current->fs->umask;
  1894. error = may_mknod(mode);
  1895. if (error)
  1896. goto out_dput;
  1897. error = mnt_want_write(nd.path.mnt);
  1898. if (error)
  1899. goto out_dput;
  1900. switch (mode & S_IFMT) {
  1901. case 0: case S_IFREG:
  1902. error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
  1903. break;
  1904. case S_IFCHR: case S_IFBLK:
  1905. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
  1906. new_decode_dev(dev));
  1907. break;
  1908. case S_IFIFO: case S_IFSOCK:
  1909. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
  1910. break;
  1911. }
  1912. mnt_drop_write(nd.path.mnt);
  1913. out_dput:
  1914. dput(dentry);
  1915. out_unlock:
  1916. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  1917. path_put(&nd.path);
  1918. out:
  1919. putname(tmp);
  1920. return error;
  1921. }
  1922. asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
  1923. {
  1924. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  1925. }
  1926. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1927. {
  1928. int error = may_create(dir, dentry, NULL);
  1929. if (error)
  1930. return error;
  1931. if (!dir->i_op || !dir->i_op->mkdir)
  1932. return -EPERM;
  1933. mode &= (S_IRWXUGO|S_ISVTX);
  1934. error = security_inode_mkdir(dir, dentry, mode);
  1935. if (error)
  1936. return error;
  1937. DQUOT_INIT(dir);
  1938. error = dir->i_op->mkdir(dir, dentry, mode);
  1939. if (!error)
  1940. fsnotify_mkdir(dir, dentry);
  1941. return error;
  1942. }
  1943. asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
  1944. {
  1945. int error = 0;
  1946. char * tmp;
  1947. struct dentry *dentry;
  1948. struct nameidata nd;
  1949. tmp = getname(pathname);
  1950. error = PTR_ERR(tmp);
  1951. if (IS_ERR(tmp))
  1952. goto out_err;
  1953. error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
  1954. if (error)
  1955. goto out;
  1956. dentry = lookup_create(&nd, 1);
  1957. error = PTR_ERR(dentry);
  1958. if (IS_ERR(dentry))
  1959. goto out_unlock;
  1960. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  1961. mode &= ~current->fs->umask;
  1962. error = mnt_want_write(nd.path.mnt);
  1963. if (error)
  1964. goto out_dput;
  1965. error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
  1966. mnt_drop_write(nd.path.mnt);
  1967. out_dput:
  1968. dput(dentry);
  1969. out_unlock:
  1970. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  1971. path_put(&nd.path);
  1972. out:
  1973. putname(tmp);
  1974. out_err:
  1975. return error;
  1976. }
  1977. asmlinkage long sys_mkdir(const char __user *pathname, int mode)
  1978. {
  1979. return sys_mkdirat(AT_FDCWD, pathname, mode);
  1980. }
  1981. /*
  1982. * We try to drop the dentry early: we should have
  1983. * a usage count of 2 if we're the only user of this
  1984. * dentry, and if that is true (possibly after pruning
  1985. * the dcache), then we drop the dentry now.
  1986. *
  1987. * A low-level filesystem can, if it choses, legally
  1988. * do a
  1989. *
  1990. * if (!d_unhashed(dentry))
  1991. * return -EBUSY;
  1992. *
  1993. * if it cannot handle the case of removing a directory
  1994. * that is still in use by something else..
  1995. */
  1996. void dentry_unhash(struct dentry *dentry)
  1997. {
  1998. dget(dentry);
  1999. shrink_dcache_parent(dentry);
  2000. spin_lock(&dcache_lock);
  2001. spin_lock(&dentry->d_lock);
  2002. if (atomic_read(&dentry->d_count) == 2)
  2003. __d_drop(dentry);
  2004. spin_unlock(&dentry->d_lock);
  2005. spin_unlock(&dcache_lock);
  2006. }
  2007. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  2008. {
  2009. int error = may_delete(dir, dentry, 1);
  2010. if (error)
  2011. return error;
  2012. if (!dir->i_op || !dir->i_op->rmdir)
  2013. return -EPERM;
  2014. DQUOT_INIT(dir);
  2015. mutex_lock(&dentry->d_inode->i_mutex);
  2016. dentry_unhash(dentry);
  2017. if (d_mountpoint(dentry))
  2018. error = -EBUSY;
  2019. else {
  2020. error = security_inode_rmdir(dir, dentry);
  2021. if (!error) {
  2022. error = dir->i_op->rmdir(dir, dentry);
  2023. if (!error)
  2024. dentry->d_inode->i_flags |= S_DEAD;
  2025. }
  2026. }
  2027. mutex_unlock(&dentry->d_inode->i_mutex);
  2028. if (!error) {
  2029. d_delete(dentry);
  2030. }
  2031. dput(dentry);
  2032. return error;
  2033. }
  2034. static long do_rmdir(int dfd, const char __user *pathname)
  2035. {
  2036. int error = 0;
  2037. char * name;
  2038. struct dentry *dentry;
  2039. struct nameidata nd;
  2040. name = getname(pathname);
  2041. if(IS_ERR(name))
  2042. return PTR_ERR(name);
  2043. error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
  2044. if (error)
  2045. goto exit;
  2046. switch(nd.last_type) {
  2047. case LAST_DOTDOT:
  2048. error = -ENOTEMPTY;
  2049. goto exit1;
  2050. case LAST_DOT:
  2051. error = -EINVAL;
  2052. goto exit1;
  2053. case LAST_ROOT:
  2054. error = -EBUSY;
  2055. goto exit1;
  2056. }
  2057. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2058. dentry = lookup_hash(&nd);
  2059. error = PTR_ERR(dentry);
  2060. if (IS_ERR(dentry))
  2061. goto exit2;
  2062. error = mnt_want_write(nd.path.mnt);
  2063. if (error)
  2064. goto exit3;
  2065. error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
  2066. mnt_drop_write(nd.path.mnt);
  2067. exit3:
  2068. dput(dentry);
  2069. exit2:
  2070. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2071. exit1:
  2072. path_put(&nd.path);
  2073. exit:
  2074. putname(name);
  2075. return error;
  2076. }
  2077. asmlinkage long sys_rmdir(const char __user *pathname)
  2078. {
  2079. return do_rmdir(AT_FDCWD, pathname);
  2080. }
  2081. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  2082. {
  2083. int error = may_delete(dir, dentry, 0);
  2084. if (error)
  2085. return error;
  2086. if (!dir->i_op || !dir->i_op->unlink)
  2087. return -EPERM;
  2088. DQUOT_INIT(dir);
  2089. mutex_lock(&dentry->d_inode->i_mutex);
  2090. if (d_mountpoint(dentry))
  2091. error = -EBUSY;
  2092. else {
  2093. error = security_inode_unlink(dir, dentry);
  2094. if (!error)
  2095. error = dir->i_op->unlink(dir, dentry);
  2096. }
  2097. mutex_unlock(&dentry->d_inode->i_mutex);
  2098. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  2099. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  2100. fsnotify_link_count(dentry->d_inode);
  2101. d_delete(dentry);
  2102. }
  2103. return error;
  2104. }
  2105. /*
  2106. * Make sure that the actual truncation of the file will occur outside its
  2107. * directory's i_mutex. Truncate can take a long time if there is a lot of
  2108. * writeout happening, and we don't want to prevent access to the directory
  2109. * while waiting on the I/O.
  2110. */
  2111. static long do_unlinkat(int dfd, const char __user *pathname)
  2112. {
  2113. int error = 0;
  2114. char * name;
  2115. struct dentry *dentry;
  2116. struct nameidata nd;
  2117. struct inode *inode = NULL;
  2118. name = getname(pathname);
  2119. if(IS_ERR(name))
  2120. return PTR_ERR(name);
  2121. error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
  2122. if (error)
  2123. goto exit;
  2124. error = -EISDIR;
  2125. if (nd.last_type != LAST_NORM)
  2126. goto exit1;
  2127. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2128. dentry = lookup_hash(&nd);
  2129. error = PTR_ERR(dentry);
  2130. if (!IS_ERR(dentry)) {
  2131. /* Why not before? Because we want correct error value */
  2132. if (nd.last.name[nd.last.len])
  2133. goto slashes;
  2134. inode = dentry->d_inode;
  2135. if (inode)
  2136. atomic_inc(&inode->i_count);
  2137. error = mnt_want_write(nd.path.mnt);
  2138. if (error)
  2139. goto exit2;
  2140. error = vfs_unlink(nd.path.dentry->d_inode, dentry);
  2141. mnt_drop_write(nd.path.mnt);
  2142. exit2:
  2143. dput(dentry);
  2144. }
  2145. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2146. if (inode)
  2147. iput(inode); /* truncate the inode here */
  2148. exit1:
  2149. path_put(&nd.path);
  2150. exit:
  2151. putname(name);
  2152. return error;
  2153. slashes:
  2154. error = !dentry->d_inode ? -ENOENT :
  2155. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  2156. goto exit2;
  2157. }
  2158. asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
  2159. {
  2160. if ((flag & ~AT_REMOVEDIR) != 0)
  2161. return -EINVAL;
  2162. if (flag & AT_REMOVEDIR)
  2163. return do_rmdir(dfd, pathname);
  2164. return do_unlinkat(dfd, pathname);
  2165. }
  2166. asmlinkage long sys_unlink(const char __user *pathname)
  2167. {
  2168. return do_unlinkat(AT_FDCWD, pathname);
  2169. }
  2170. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  2171. {
  2172. int error = may_create(dir, dentry, NULL);
  2173. if (error)
  2174. return error;
  2175. if (!dir->i_op || !dir->i_op->symlink)
  2176. return -EPERM;
  2177. error = security_inode_symlink(dir, dentry, oldname);
  2178. if (error)
  2179. return error;
  2180. DQUOT_INIT(dir);
  2181. error = dir->i_op->symlink(dir, dentry, oldname);
  2182. if (!error)
  2183. fsnotify_create(dir, dentry);
  2184. return error;
  2185. }
  2186. asmlinkage long sys_symlinkat(const char __user *oldname,
  2187. int newdfd, const char __user *newname)
  2188. {
  2189. int error = 0;
  2190. char * from;
  2191. char * to;
  2192. struct dentry *dentry;
  2193. struct nameidata nd;
  2194. from = getname(oldname);
  2195. if(IS_ERR(from))
  2196. return PTR_ERR(from);
  2197. to = getname(newname);
  2198. error = PTR_ERR(to);
  2199. if (IS_ERR(to))
  2200. goto out_putname;
  2201. error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
  2202. if (error)
  2203. goto out;
  2204. dentry = lookup_create(&nd, 0);
  2205. error = PTR_ERR(dentry);
  2206. if (IS_ERR(dentry))
  2207. goto out_unlock;
  2208. error = mnt_want_write(nd.path.mnt);
  2209. if (error)
  2210. goto out_dput;
  2211. error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
  2212. mnt_drop_write(nd.path.mnt);
  2213. out_dput:
  2214. dput(dentry);
  2215. out_unlock:
  2216. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2217. path_put(&nd.path);
  2218. out:
  2219. putname(to);
  2220. out_putname:
  2221. putname(from);
  2222. return error;
  2223. }
  2224. asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
  2225. {
  2226. return sys_symlinkat(oldname, AT_FDCWD, newname);
  2227. }
  2228. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2229. {
  2230. struct inode *inode = old_dentry->d_inode;
  2231. int error;
  2232. if (!inode)
  2233. return -ENOENT;
  2234. error = may_create(dir, new_dentry, NULL);
  2235. if (error)
  2236. return error;
  2237. if (dir->i_sb != inode->i_sb)
  2238. return -EXDEV;
  2239. /*
  2240. * A link to an append-only or immutable file cannot be created.
  2241. */
  2242. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2243. return -EPERM;
  2244. if (!dir->i_op || !dir->i_op->link)
  2245. return -EPERM;
  2246. if (S_ISDIR(inode->i_mode))
  2247. return -EPERM;
  2248. error = security_inode_link(old_dentry, dir, new_dentry);
  2249. if (error)
  2250. return error;
  2251. mutex_lock(&inode->i_mutex);
  2252. DQUOT_INIT(dir);
  2253. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2254. mutex_unlock(&inode->i_mutex);
  2255. if (!error)
  2256. fsnotify_link(dir, inode, new_dentry);
  2257. return error;
  2258. }
  2259. /*
  2260. * Hardlinks are often used in delicate situations. We avoid
  2261. * security-related surprises by not following symlinks on the
  2262. * newname. --KAB
  2263. *
  2264. * We don't follow them on the oldname either to be compatible
  2265. * with linux 2.0, and to avoid hard-linking to directories
  2266. * and other special files. --ADM
  2267. */
  2268. asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
  2269. int newdfd, const char __user *newname,
  2270. int flags)
  2271. {
  2272. struct dentry *new_dentry;
  2273. struct nameidata nd, old_nd;
  2274. int error;
  2275. char * to;
  2276. if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
  2277. return -EINVAL;
  2278. to = getname(newname);
  2279. if (IS_ERR(to))
  2280. return PTR_ERR(to);
  2281. error = __user_walk_fd(olddfd, oldname,
  2282. flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
  2283. &old_nd);
  2284. if (error)
  2285. goto exit;
  2286. error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
  2287. if (error)
  2288. goto out;
  2289. error = -EXDEV;
  2290. if (old_nd.path.mnt != nd.path.mnt)
  2291. goto out_release;
  2292. new_dentry = lookup_create(&nd, 0);
  2293. error = PTR_ERR(new_dentry);
  2294. if (IS_ERR(new_dentry))
  2295. goto out_unlock;
  2296. error = mnt_want_write(nd.path.mnt);
  2297. if (error)
  2298. goto out_dput;
  2299. error = vfs_link(old_nd.path.dentry, nd.path.dentry->d_inode, new_dentry);
  2300. mnt_drop_write(nd.path.mnt);
  2301. out_dput:
  2302. dput(new_dentry);
  2303. out_unlock:
  2304. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2305. out_release:
  2306. path_put(&nd.path);
  2307. out:
  2308. path_put(&old_nd.path);
  2309. exit:
  2310. putname(to);
  2311. return error;
  2312. }
  2313. asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
  2314. {
  2315. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2316. }
  2317. /*
  2318. * The worst of all namespace operations - renaming directory. "Perverted"
  2319. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2320. * Problems:
  2321. * a) we can get into loop creation. Check is done in is_subdir().
  2322. * b) race potential - two innocent renames can create a loop together.
  2323. * That's where 4.4 screws up. Current fix: serialization on
  2324. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2325. * story.
  2326. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2327. * And that - after we got ->i_mutex on parents (until then we don't know
  2328. * whether the target exists). Solution: try to be smart with locking
  2329. * order for inodes. We rely on the fact that tree topology may change
  2330. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2331. * move will be locked. Thus we can rank directories by the tree
  2332. * (ancestors first) and rank all non-directories after them.
  2333. * That works since everybody except rename does "lock parent, lookup,
  2334. * lock child" and rename is under ->s_vfs_rename_mutex.
  2335. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2336. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2337. * we'd better make sure that there's no link(2) for them.
  2338. * d) some filesystems don't support opened-but-unlinked directories,
  2339. * either because of layout or because they are not ready to deal with
  2340. * all cases correctly. The latter will be fixed (taking this sort of
  2341. * stuff into VFS), but the former is not going away. Solution: the same
  2342. * trick as in rmdir().
  2343. * e) conversion from fhandle to dentry may come in the wrong moment - when
  2344. * we are removing the target. Solution: we will have to grab ->i_mutex
  2345. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2346. * ->i_mutex on parents, which works but leads to some truely excessive
  2347. * locking].
  2348. */
  2349. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2350. struct inode *new_dir, struct dentry *new_dentry)
  2351. {
  2352. int error = 0;
  2353. struct inode *target;
  2354. /*
  2355. * If we are going to change the parent - check write permissions,
  2356. * we'll need to flip '..'.
  2357. */
  2358. if (new_dir != old_dir) {
  2359. error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
  2360. if (error)
  2361. return error;
  2362. }
  2363. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2364. if (error)
  2365. return error;
  2366. target = new_dentry->d_inode;
  2367. if (target) {
  2368. mutex_lock(&target->i_mutex);
  2369. dentry_unhash(new_dentry);
  2370. }
  2371. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2372. error = -EBUSY;
  2373. else
  2374. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2375. if (target) {
  2376. if (!error)
  2377. target->i_flags |= S_DEAD;
  2378. mutex_unlock(&target->i_mutex);
  2379. if (d_unhashed(new_dentry))
  2380. d_rehash(new_dentry);
  2381. dput(new_dentry);
  2382. }
  2383. if (!error)
  2384. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2385. d_move(old_dentry,new_dentry);
  2386. return error;
  2387. }
  2388. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2389. struct inode *new_dir, struct dentry *new_dentry)
  2390. {
  2391. struct inode *target;
  2392. int error;
  2393. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2394. if (error)
  2395. return error;
  2396. dget(new_dentry);
  2397. target = new_dentry->d_inode;
  2398. if (target)
  2399. mutex_lock(&target->i_mutex);
  2400. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2401. error = -EBUSY;
  2402. else
  2403. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2404. if (!error) {
  2405. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2406. d_move(old_dentry, new_dentry);
  2407. }
  2408. if (target)
  2409. mutex_unlock(&target->i_mutex);
  2410. dput(new_dentry);
  2411. return error;
  2412. }
  2413. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2414. struct inode *new_dir, struct dentry *new_dentry)
  2415. {
  2416. int error;
  2417. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2418. const char *old_name;
  2419. if (old_dentry->d_inode == new_dentry->d_inode)
  2420. return 0;
  2421. error = may_delete(old_dir, old_dentry, is_dir);
  2422. if (error)
  2423. return error;
  2424. if (!new_dentry->d_inode)
  2425. error = may_create(new_dir, new_dentry, NULL);
  2426. else
  2427. error = may_delete(new_dir, new_dentry, is_dir);
  2428. if (error)
  2429. return error;
  2430. if (!old_dir->i_op || !old_dir->i_op->rename)
  2431. return -EPERM;
  2432. DQUOT_INIT(old_dir);
  2433. DQUOT_INIT(new_dir);
  2434. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2435. if (is_dir)
  2436. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2437. else
  2438. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2439. if (!error) {
  2440. const char *new_name = old_dentry->d_name.name;
  2441. fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
  2442. new_dentry->d_inode, old_dentry);
  2443. }
  2444. fsnotify_oldname_free(old_name);
  2445. return error;
  2446. }
  2447. static int do_rename(int olddfd, const char *oldname,
  2448. int newdfd, const char *newname)
  2449. {
  2450. int error = 0;
  2451. struct dentry * old_dir, * new_dir;
  2452. struct dentry * old_dentry, *new_dentry;
  2453. struct dentry * trap;
  2454. struct nameidata oldnd, newnd;
  2455. error = do_path_lookup(olddfd, oldname, LOOKUP_PARENT, &oldnd);
  2456. if (error)
  2457. goto exit;
  2458. error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
  2459. if (error)
  2460. goto exit1;
  2461. error = -EXDEV;
  2462. if (oldnd.path.mnt != newnd.path.mnt)
  2463. goto exit2;
  2464. old_dir = oldnd.path.dentry;
  2465. error = -EBUSY;
  2466. if (oldnd.last_type != LAST_NORM)
  2467. goto exit2;
  2468. new_dir = newnd.path.dentry;
  2469. if (newnd.last_type != LAST_NORM)
  2470. goto exit2;
  2471. trap = lock_rename(new_dir, old_dir);
  2472. old_dentry = lookup_hash(&oldnd);
  2473. error = PTR_ERR(old_dentry);
  2474. if (IS_ERR(old_dentry))
  2475. goto exit3;
  2476. /* source must exist */
  2477. error = -ENOENT;
  2478. if (!old_dentry->d_inode)
  2479. goto exit4;
  2480. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2481. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2482. error = -ENOTDIR;
  2483. if (oldnd.last.name[oldnd.last.len])
  2484. goto exit4;
  2485. if (newnd.last.name[newnd.last.len])
  2486. goto exit4;
  2487. }
  2488. /* source should not be ancestor of target */
  2489. error = -EINVAL;
  2490. if (old_dentry == trap)
  2491. goto exit4;
  2492. new_dentry = lookup_hash(&newnd);
  2493. error = PTR_ERR(new_dentry);
  2494. if (IS_ERR(new_dentry))
  2495. goto exit4;
  2496. /* target should not be an ancestor of source */
  2497. error = -ENOTEMPTY;
  2498. if (new_dentry == trap)
  2499. goto exit5;
  2500. error = mnt_want_write(oldnd.path.mnt);
  2501. if (error)
  2502. goto exit5;
  2503. error = vfs_rename(old_dir->d_inode, old_dentry,
  2504. new_dir->d_inode, new_dentry);
  2505. mnt_drop_write(oldnd.path.mnt);
  2506. exit5:
  2507. dput(new_dentry);
  2508. exit4:
  2509. dput(old_dentry);
  2510. exit3:
  2511. unlock_rename(new_dir, old_dir);
  2512. exit2:
  2513. path_put(&newnd.path);
  2514. exit1:
  2515. path_put(&oldnd.path);
  2516. exit:
  2517. return error;
  2518. }
  2519. asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
  2520. int newdfd, const char __user *newname)
  2521. {
  2522. int error;
  2523. char * from;
  2524. char * to;
  2525. from = getname(oldname);
  2526. if(IS_ERR(from))
  2527. return PTR_ERR(from);
  2528. to = getname(newname);
  2529. error = PTR_ERR(to);
  2530. if (!IS_ERR(to)) {
  2531. error = do_rename(olddfd, from, newdfd, to);
  2532. putname(to);
  2533. }
  2534. putname(from);
  2535. return error;
  2536. }
  2537. asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
  2538. {
  2539. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2540. }
  2541. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2542. {
  2543. int len;
  2544. len = PTR_ERR(link);
  2545. if (IS_ERR(link))
  2546. goto out;
  2547. len = strlen(link);
  2548. if (len > (unsigned) buflen)
  2549. len = buflen;
  2550. if (copy_to_user(buffer, link, len))
  2551. len = -EFAULT;
  2552. out:
  2553. return len;
  2554. }
  2555. /*
  2556. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2557. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2558. * using) it for any given inode is up to filesystem.
  2559. */
  2560. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2561. {
  2562. struct nameidata nd;
  2563. void *cookie;
  2564. int res;
  2565. nd.depth = 0;
  2566. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2567. if (IS_ERR(cookie))
  2568. return PTR_ERR(cookie);
  2569. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2570. if (dentry->d_inode->i_op->put_link)
  2571. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2572. return res;
  2573. }
  2574. int vfs_follow_link(struct nameidata *nd, const char *link)
  2575. {
  2576. return __vfs_follow_link(nd, link);
  2577. }
  2578. /* get the link contents into pagecache */
  2579. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2580. {
  2581. struct page * page;
  2582. struct address_space *mapping = dentry->d_inode->i_mapping;
  2583. page = read_mapping_page(mapping, 0, NULL);
  2584. if (IS_ERR(page))
  2585. return (char*)page;
  2586. *ppage = page;
  2587. return kmap(page);
  2588. }
  2589. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2590. {
  2591. struct page *page = NULL;
  2592. char *s = page_getlink(dentry, &page);
  2593. int res = vfs_readlink(dentry,buffer,buflen,s);
  2594. if (page) {
  2595. kunmap(page);
  2596. page_cache_release(page);
  2597. }
  2598. return res;
  2599. }
  2600. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2601. {
  2602. struct page *page = NULL;
  2603. nd_set_link(nd, page_getlink(dentry, &page));
  2604. return page;
  2605. }
  2606. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2607. {
  2608. struct page *page = cookie;
  2609. if (page) {
  2610. kunmap(page);
  2611. page_cache_release(page);
  2612. }
  2613. }
  2614. int __page_symlink(struct inode *inode, const char *symname, int len,
  2615. gfp_t gfp_mask)
  2616. {
  2617. struct address_space *mapping = inode->i_mapping;
  2618. struct page *page;
  2619. void *fsdata;
  2620. int err;
  2621. char *kaddr;
  2622. retry:
  2623. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  2624. AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
  2625. if (err)
  2626. goto fail;
  2627. kaddr = kmap_atomic(page, KM_USER0);
  2628. memcpy(kaddr, symname, len-1);
  2629. kunmap_atomic(kaddr, KM_USER0);
  2630. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  2631. page, fsdata);
  2632. if (err < 0)
  2633. goto fail;
  2634. if (err < len-1)
  2635. goto retry;
  2636. mark_inode_dirty(inode);
  2637. return 0;
  2638. fail:
  2639. return err;
  2640. }
  2641. int page_symlink(struct inode *inode, const char *symname, int len)
  2642. {
  2643. return __page_symlink(inode, symname, len,
  2644. mapping_gfp_mask(inode->i_mapping));
  2645. }
  2646. const struct inode_operations page_symlink_inode_operations = {
  2647. .readlink = generic_readlink,
  2648. .follow_link = page_follow_link_light,
  2649. .put_link = page_put_link,
  2650. };
  2651. EXPORT_SYMBOL(__user_walk);
  2652. EXPORT_SYMBOL(__user_walk_fd);
  2653. EXPORT_SYMBOL(follow_down);
  2654. EXPORT_SYMBOL(follow_up);
  2655. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2656. EXPORT_SYMBOL(getname);
  2657. EXPORT_SYMBOL(lock_rename);
  2658. EXPORT_SYMBOL(lookup_one_len);
  2659. EXPORT_SYMBOL(page_follow_link_light);
  2660. EXPORT_SYMBOL(page_put_link);
  2661. EXPORT_SYMBOL(page_readlink);
  2662. EXPORT_SYMBOL(__page_symlink);
  2663. EXPORT_SYMBOL(page_symlink);
  2664. EXPORT_SYMBOL(page_symlink_inode_operations);
  2665. EXPORT_SYMBOL(path_lookup);
  2666. EXPORT_SYMBOL(vfs_path_lookup);
  2667. EXPORT_SYMBOL(permission);
  2668. EXPORT_SYMBOL(vfs_permission);
  2669. EXPORT_SYMBOL(file_permission);
  2670. EXPORT_SYMBOL(unlock_rename);
  2671. EXPORT_SYMBOL(vfs_create);
  2672. EXPORT_SYMBOL(vfs_follow_link);
  2673. EXPORT_SYMBOL(vfs_link);
  2674. EXPORT_SYMBOL(vfs_mkdir);
  2675. EXPORT_SYMBOL(vfs_mknod);
  2676. EXPORT_SYMBOL(generic_permission);
  2677. EXPORT_SYMBOL(vfs_readlink);
  2678. EXPORT_SYMBOL(vfs_rename);
  2679. EXPORT_SYMBOL(vfs_rmdir);
  2680. EXPORT_SYMBOL(vfs_symlink);
  2681. EXPORT_SYMBOL(vfs_unlink);
  2682. EXPORT_SYMBOL(dentry_unhash);
  2683. EXPORT_SYMBOL(generic_readlink);