namei.c 63 KB

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