namei.c 68 KB

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