namei.c 68 KB

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