namei.c 60 KB

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