namei.c 69 KB

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