namespace.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772
  1. /*
  2. * linux/fs/namespace.c
  3. *
  4. * (C) Copyright Al Viro 2000, 2001
  5. * Released under GPL v2.
  6. *
  7. * Based on code from fs/super.c, copyright Linus Torvalds and others.
  8. * Heavily rewritten.
  9. */
  10. #include <linux/syscalls.h>
  11. #include <linux/slab.h>
  12. #include <linux/sched.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/percpu.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/acct.h>
  18. #include <linux/capability.h>
  19. #include <linux/cpumask.h>
  20. #include <linux/module.h>
  21. #include <linux/sysfs.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/mnt_namespace.h>
  24. #include <linux/namei.h>
  25. #include <linux/nsproxy.h>
  26. #include <linux/security.h>
  27. #include <linux/mount.h>
  28. #include <linux/ramfs.h>
  29. #include <linux/log2.h>
  30. #include <linux/idr.h>
  31. #include <linux/fs_struct.h>
  32. #include <linux/fsnotify.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/unistd.h>
  35. #include "pnode.h"
  36. #include "internal.h"
  37. #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
  38. #define HASH_SIZE (1UL << HASH_SHIFT)
  39. static int event;
  40. static DEFINE_IDA(mnt_id_ida);
  41. static DEFINE_IDA(mnt_group_ida);
  42. static DEFINE_SPINLOCK(mnt_id_lock);
  43. static int mnt_id_start = 0;
  44. static int mnt_group_start = 1;
  45. static struct list_head *mount_hashtable __read_mostly;
  46. static struct kmem_cache *mnt_cache __read_mostly;
  47. static struct rw_semaphore namespace_sem;
  48. /* /sys/fs */
  49. struct kobject *fs_kobj;
  50. EXPORT_SYMBOL_GPL(fs_kobj);
  51. /*
  52. * vfsmount lock may be taken for read to prevent changes to the
  53. * vfsmount hash, ie. during mountpoint lookups or walking back
  54. * up the tree.
  55. *
  56. * It should be taken for write in all cases where the vfsmount
  57. * tree or hash is modified or when a vfsmount structure is modified.
  58. */
  59. DEFINE_BRLOCK(vfsmount_lock);
  60. static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
  61. {
  62. unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
  63. tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
  64. tmp = tmp + (tmp >> HASH_SHIFT);
  65. return tmp & (HASH_SIZE - 1);
  66. }
  67. #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
  68. /*
  69. * allocation is serialized by namespace_sem, but we need the spinlock to
  70. * serialize with freeing.
  71. */
  72. static int mnt_alloc_id(struct vfsmount *mnt)
  73. {
  74. int res;
  75. retry:
  76. ida_pre_get(&mnt_id_ida, GFP_KERNEL);
  77. spin_lock(&mnt_id_lock);
  78. res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
  79. if (!res)
  80. mnt_id_start = mnt->mnt_id + 1;
  81. spin_unlock(&mnt_id_lock);
  82. if (res == -EAGAIN)
  83. goto retry;
  84. return res;
  85. }
  86. static void mnt_free_id(struct vfsmount *mnt)
  87. {
  88. int id = mnt->mnt_id;
  89. spin_lock(&mnt_id_lock);
  90. ida_remove(&mnt_id_ida, id);
  91. if (mnt_id_start > id)
  92. mnt_id_start = id;
  93. spin_unlock(&mnt_id_lock);
  94. }
  95. /*
  96. * Allocate a new peer group ID
  97. *
  98. * mnt_group_ida is protected by namespace_sem
  99. */
  100. static int mnt_alloc_group_id(struct vfsmount *mnt)
  101. {
  102. int res;
  103. if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
  104. return -ENOMEM;
  105. res = ida_get_new_above(&mnt_group_ida,
  106. mnt_group_start,
  107. &mnt->mnt_group_id);
  108. if (!res)
  109. mnt_group_start = mnt->mnt_group_id + 1;
  110. return res;
  111. }
  112. /*
  113. * Release a peer group ID
  114. */
  115. void mnt_release_group_id(struct vfsmount *mnt)
  116. {
  117. int id = mnt->mnt_group_id;
  118. ida_remove(&mnt_group_ida, id);
  119. if (mnt_group_start > id)
  120. mnt_group_start = id;
  121. mnt->mnt_group_id = 0;
  122. }
  123. /*
  124. * vfsmount lock must be held for read
  125. */
  126. static inline void mnt_add_count(struct vfsmount *mnt, int n)
  127. {
  128. #ifdef CONFIG_SMP
  129. this_cpu_add(mnt->mnt_pcp->mnt_count, n);
  130. #else
  131. preempt_disable();
  132. mnt->mnt_count += n;
  133. preempt_enable();
  134. #endif
  135. }
  136. /*
  137. * vfsmount lock must be held for write
  138. */
  139. unsigned int mnt_get_count(struct vfsmount *mnt)
  140. {
  141. #ifdef CONFIG_SMP
  142. unsigned int count = 0;
  143. int cpu;
  144. for_each_possible_cpu(cpu) {
  145. count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
  146. }
  147. return count;
  148. #else
  149. return mnt->mnt_count;
  150. #endif
  151. }
  152. static struct vfsmount *alloc_vfsmnt(const char *name)
  153. {
  154. struct mount *p = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
  155. if (p) {
  156. struct vfsmount *mnt = &p->mnt;
  157. int err;
  158. err = mnt_alloc_id(mnt);
  159. if (err)
  160. goto out_free_cache;
  161. if (name) {
  162. mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
  163. if (!mnt->mnt_devname)
  164. goto out_free_id;
  165. }
  166. #ifdef CONFIG_SMP
  167. mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
  168. if (!mnt->mnt_pcp)
  169. goto out_free_devname;
  170. this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
  171. #else
  172. mnt->mnt_count = 1;
  173. mnt->mnt_writers = 0;
  174. #endif
  175. INIT_LIST_HEAD(&mnt->mnt_hash);
  176. INIT_LIST_HEAD(&mnt->mnt_child);
  177. INIT_LIST_HEAD(&mnt->mnt_mounts);
  178. INIT_LIST_HEAD(&mnt->mnt_list);
  179. INIT_LIST_HEAD(&mnt->mnt_expire);
  180. INIT_LIST_HEAD(&mnt->mnt_share);
  181. INIT_LIST_HEAD(&mnt->mnt_slave_list);
  182. INIT_LIST_HEAD(&mnt->mnt_slave);
  183. #ifdef CONFIG_FSNOTIFY
  184. INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
  185. #endif
  186. }
  187. return &p->mnt;
  188. #ifdef CONFIG_SMP
  189. out_free_devname:
  190. kfree(p->mnt.mnt_devname);
  191. #endif
  192. out_free_id:
  193. mnt_free_id(&p->mnt);
  194. out_free_cache:
  195. kmem_cache_free(mnt_cache, p);
  196. return NULL;
  197. }
  198. /*
  199. * Most r/o checks on a fs are for operations that take
  200. * discrete amounts of time, like a write() or unlink().
  201. * We must keep track of when those operations start
  202. * (for permission checks) and when they end, so that
  203. * we can determine when writes are able to occur to
  204. * a filesystem.
  205. */
  206. /*
  207. * __mnt_is_readonly: check whether a mount is read-only
  208. * @mnt: the mount to check for its write status
  209. *
  210. * This shouldn't be used directly ouside of the VFS.
  211. * It does not guarantee that the filesystem will stay
  212. * r/w, just that it is right *now*. This can not and
  213. * should not be used in place of IS_RDONLY(inode).
  214. * mnt_want/drop_write() will _keep_ the filesystem
  215. * r/w.
  216. */
  217. int __mnt_is_readonly(struct vfsmount *mnt)
  218. {
  219. if (mnt->mnt_flags & MNT_READONLY)
  220. return 1;
  221. if (mnt->mnt_sb->s_flags & MS_RDONLY)
  222. return 1;
  223. return 0;
  224. }
  225. EXPORT_SYMBOL_GPL(__mnt_is_readonly);
  226. static inline void mnt_inc_writers(struct vfsmount *mnt)
  227. {
  228. #ifdef CONFIG_SMP
  229. this_cpu_inc(mnt->mnt_pcp->mnt_writers);
  230. #else
  231. mnt->mnt_writers++;
  232. #endif
  233. }
  234. static inline void mnt_dec_writers(struct vfsmount *mnt)
  235. {
  236. #ifdef CONFIG_SMP
  237. this_cpu_dec(mnt->mnt_pcp->mnt_writers);
  238. #else
  239. mnt->mnt_writers--;
  240. #endif
  241. }
  242. static unsigned int mnt_get_writers(struct vfsmount *mnt)
  243. {
  244. #ifdef CONFIG_SMP
  245. unsigned int count = 0;
  246. int cpu;
  247. for_each_possible_cpu(cpu) {
  248. count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
  249. }
  250. return count;
  251. #else
  252. return mnt->mnt_writers;
  253. #endif
  254. }
  255. /*
  256. * Most r/o checks on a fs are for operations that take
  257. * discrete amounts of time, like a write() or unlink().
  258. * We must keep track of when those operations start
  259. * (for permission checks) and when they end, so that
  260. * we can determine when writes are able to occur to
  261. * a filesystem.
  262. */
  263. /**
  264. * mnt_want_write - get write access to a mount
  265. * @mnt: the mount on which to take a write
  266. *
  267. * This tells the low-level filesystem that a write is
  268. * about to be performed to it, and makes sure that
  269. * writes are allowed before returning success. When
  270. * the write operation is finished, mnt_drop_write()
  271. * must be called. This is effectively a refcount.
  272. */
  273. int mnt_want_write(struct vfsmount *mnt)
  274. {
  275. int ret = 0;
  276. preempt_disable();
  277. mnt_inc_writers(mnt);
  278. /*
  279. * The store to mnt_inc_writers must be visible before we pass
  280. * MNT_WRITE_HOLD loop below, so that the slowpath can see our
  281. * incremented count after it has set MNT_WRITE_HOLD.
  282. */
  283. smp_mb();
  284. while (mnt->mnt_flags & MNT_WRITE_HOLD)
  285. cpu_relax();
  286. /*
  287. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  288. * be set to match its requirements. So we must not load that until
  289. * MNT_WRITE_HOLD is cleared.
  290. */
  291. smp_rmb();
  292. if (__mnt_is_readonly(mnt)) {
  293. mnt_dec_writers(mnt);
  294. ret = -EROFS;
  295. goto out;
  296. }
  297. out:
  298. preempt_enable();
  299. return ret;
  300. }
  301. EXPORT_SYMBOL_GPL(mnt_want_write);
  302. /**
  303. * mnt_clone_write - get write access to a mount
  304. * @mnt: the mount on which to take a write
  305. *
  306. * This is effectively like mnt_want_write, except
  307. * it must only be used to take an extra write reference
  308. * on a mountpoint that we already know has a write reference
  309. * on it. This allows some optimisation.
  310. *
  311. * After finished, mnt_drop_write must be called as usual to
  312. * drop the reference.
  313. */
  314. int mnt_clone_write(struct vfsmount *mnt)
  315. {
  316. /* superblock may be r/o */
  317. if (__mnt_is_readonly(mnt))
  318. return -EROFS;
  319. preempt_disable();
  320. mnt_inc_writers(mnt);
  321. preempt_enable();
  322. return 0;
  323. }
  324. EXPORT_SYMBOL_GPL(mnt_clone_write);
  325. /**
  326. * mnt_want_write_file - get write access to a file's mount
  327. * @file: the file who's mount on which to take a write
  328. *
  329. * This is like mnt_want_write, but it takes a file and can
  330. * do some optimisations if the file is open for write already
  331. */
  332. int mnt_want_write_file(struct file *file)
  333. {
  334. struct inode *inode = file->f_dentry->d_inode;
  335. if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode))
  336. return mnt_want_write(file->f_path.mnt);
  337. else
  338. return mnt_clone_write(file->f_path.mnt);
  339. }
  340. EXPORT_SYMBOL_GPL(mnt_want_write_file);
  341. /**
  342. * mnt_drop_write - give up write access to a mount
  343. * @mnt: the mount on which to give up write access
  344. *
  345. * Tells the low-level filesystem that we are done
  346. * performing writes to it. Must be matched with
  347. * mnt_want_write() call above.
  348. */
  349. void mnt_drop_write(struct vfsmount *mnt)
  350. {
  351. preempt_disable();
  352. mnt_dec_writers(mnt);
  353. preempt_enable();
  354. }
  355. EXPORT_SYMBOL_GPL(mnt_drop_write);
  356. void mnt_drop_write_file(struct file *file)
  357. {
  358. mnt_drop_write(file->f_path.mnt);
  359. }
  360. EXPORT_SYMBOL(mnt_drop_write_file);
  361. static int mnt_make_readonly(struct vfsmount *mnt)
  362. {
  363. int ret = 0;
  364. br_write_lock(vfsmount_lock);
  365. mnt->mnt_flags |= MNT_WRITE_HOLD;
  366. /*
  367. * After storing MNT_WRITE_HOLD, we'll read the counters. This store
  368. * should be visible before we do.
  369. */
  370. smp_mb();
  371. /*
  372. * With writers on hold, if this value is zero, then there are
  373. * definitely no active writers (although held writers may subsequently
  374. * increment the count, they'll have to wait, and decrement it after
  375. * seeing MNT_READONLY).
  376. *
  377. * It is OK to have counter incremented on one CPU and decremented on
  378. * another: the sum will add up correctly. The danger would be when we
  379. * sum up each counter, if we read a counter before it is incremented,
  380. * but then read another CPU's count which it has been subsequently
  381. * decremented from -- we would see more decrements than we should.
  382. * MNT_WRITE_HOLD protects against this scenario, because
  383. * mnt_want_write first increments count, then smp_mb, then spins on
  384. * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
  385. * we're counting up here.
  386. */
  387. if (mnt_get_writers(mnt) > 0)
  388. ret = -EBUSY;
  389. else
  390. mnt->mnt_flags |= MNT_READONLY;
  391. /*
  392. * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
  393. * that become unheld will see MNT_READONLY.
  394. */
  395. smp_wmb();
  396. mnt->mnt_flags &= ~MNT_WRITE_HOLD;
  397. br_write_unlock(vfsmount_lock);
  398. return ret;
  399. }
  400. static void __mnt_unmake_readonly(struct vfsmount *mnt)
  401. {
  402. br_write_lock(vfsmount_lock);
  403. mnt->mnt_flags &= ~MNT_READONLY;
  404. br_write_unlock(vfsmount_lock);
  405. }
  406. static void free_vfsmnt(struct vfsmount *mnt)
  407. {
  408. struct mount *p = real_mount(mnt);
  409. kfree(mnt->mnt_devname);
  410. mnt_free_id(mnt);
  411. #ifdef CONFIG_SMP
  412. free_percpu(mnt->mnt_pcp);
  413. #endif
  414. kmem_cache_free(mnt_cache, p);
  415. }
  416. /*
  417. * find the first or last mount at @dentry on vfsmount @mnt depending on
  418. * @dir. If @dir is set return the first mount else return the last mount.
  419. * vfsmount_lock must be held for read or write.
  420. */
  421. struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
  422. int dir)
  423. {
  424. struct list_head *head = mount_hashtable + hash(mnt, dentry);
  425. struct list_head *tmp = head;
  426. struct vfsmount *p, *found = NULL;
  427. for (;;) {
  428. tmp = dir ? tmp->next : tmp->prev;
  429. p = NULL;
  430. if (tmp == head)
  431. break;
  432. p = list_entry(tmp, struct vfsmount, mnt_hash);
  433. if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
  434. found = p;
  435. break;
  436. }
  437. }
  438. return found;
  439. }
  440. /*
  441. * lookup_mnt increments the ref count before returning
  442. * the vfsmount struct.
  443. */
  444. struct vfsmount *lookup_mnt(struct path *path)
  445. {
  446. struct vfsmount *child_mnt;
  447. br_read_lock(vfsmount_lock);
  448. if ((child_mnt = __lookup_mnt(path->mnt, path->dentry, 1)))
  449. mntget(child_mnt);
  450. br_read_unlock(vfsmount_lock);
  451. return child_mnt;
  452. }
  453. static inline int check_mnt(struct vfsmount *mnt)
  454. {
  455. return mnt->mnt_ns == current->nsproxy->mnt_ns;
  456. }
  457. /*
  458. * vfsmount lock must be held for write
  459. */
  460. static void touch_mnt_namespace(struct mnt_namespace *ns)
  461. {
  462. if (ns) {
  463. ns->event = ++event;
  464. wake_up_interruptible(&ns->poll);
  465. }
  466. }
  467. /*
  468. * vfsmount lock must be held for write
  469. */
  470. static void __touch_mnt_namespace(struct mnt_namespace *ns)
  471. {
  472. if (ns && ns->event != event) {
  473. ns->event = event;
  474. wake_up_interruptible(&ns->poll);
  475. }
  476. }
  477. /*
  478. * Clear dentry's mounted state if it has no remaining mounts.
  479. * vfsmount_lock must be held for write.
  480. */
  481. static void dentry_reset_mounted(struct dentry *dentry)
  482. {
  483. unsigned u;
  484. for (u = 0; u < HASH_SIZE; u++) {
  485. struct vfsmount *p;
  486. list_for_each_entry(p, &mount_hashtable[u], mnt_hash) {
  487. if (p->mnt_mountpoint == dentry)
  488. return;
  489. }
  490. }
  491. spin_lock(&dentry->d_lock);
  492. dentry->d_flags &= ~DCACHE_MOUNTED;
  493. spin_unlock(&dentry->d_lock);
  494. }
  495. /*
  496. * vfsmount lock must be held for write
  497. */
  498. static void detach_mnt(struct vfsmount *mnt, struct path *old_path)
  499. {
  500. old_path->dentry = mnt->mnt_mountpoint;
  501. old_path->mnt = mnt->mnt_parent;
  502. mnt->mnt_parent = mnt;
  503. mnt->mnt_mountpoint = mnt->mnt_root;
  504. list_del_init(&mnt->mnt_child);
  505. list_del_init(&mnt->mnt_hash);
  506. dentry_reset_mounted(old_path->dentry);
  507. }
  508. /*
  509. * vfsmount lock must be held for write
  510. */
  511. void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry,
  512. struct vfsmount *child_mnt)
  513. {
  514. child_mnt->mnt_parent = mntget(mnt);
  515. child_mnt->mnt_mountpoint = dget(dentry);
  516. spin_lock(&dentry->d_lock);
  517. dentry->d_flags |= DCACHE_MOUNTED;
  518. spin_unlock(&dentry->d_lock);
  519. }
  520. /*
  521. * vfsmount lock must be held for write
  522. */
  523. static void attach_mnt(struct vfsmount *mnt, struct path *path)
  524. {
  525. mnt_set_mountpoint(path->mnt, path->dentry, mnt);
  526. list_add_tail(&mnt->mnt_hash, mount_hashtable +
  527. hash(path->mnt, path->dentry));
  528. list_add_tail(&mnt->mnt_child, &path->mnt->mnt_mounts);
  529. }
  530. static inline void __mnt_make_longterm(struct vfsmount *mnt)
  531. {
  532. #ifdef CONFIG_SMP
  533. atomic_inc(&mnt->mnt_longterm);
  534. #endif
  535. }
  536. /* needs vfsmount lock for write */
  537. static inline void __mnt_make_shortterm(struct vfsmount *mnt)
  538. {
  539. #ifdef CONFIG_SMP
  540. atomic_dec(&mnt->mnt_longterm);
  541. #endif
  542. }
  543. /*
  544. * vfsmount lock must be held for write
  545. */
  546. static void commit_tree(struct vfsmount *mnt)
  547. {
  548. struct vfsmount *parent = mnt->mnt_parent;
  549. struct vfsmount *m;
  550. LIST_HEAD(head);
  551. struct mnt_namespace *n = parent->mnt_ns;
  552. BUG_ON(parent == mnt);
  553. list_add_tail(&head, &mnt->mnt_list);
  554. list_for_each_entry(m, &head, mnt_list) {
  555. m->mnt_ns = n;
  556. __mnt_make_longterm(m);
  557. }
  558. list_splice(&head, n->list.prev);
  559. list_add_tail(&mnt->mnt_hash, mount_hashtable +
  560. hash(parent, mnt->mnt_mountpoint));
  561. list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
  562. touch_mnt_namespace(n);
  563. }
  564. static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
  565. {
  566. struct list_head *next = p->mnt_mounts.next;
  567. if (next == &p->mnt_mounts) {
  568. while (1) {
  569. if (p == root)
  570. return NULL;
  571. next = p->mnt_child.next;
  572. if (next != &p->mnt_parent->mnt_mounts)
  573. break;
  574. p = p->mnt_parent;
  575. }
  576. }
  577. return list_entry(next, struct vfsmount, mnt_child);
  578. }
  579. static struct vfsmount *skip_mnt_tree(struct vfsmount *p)
  580. {
  581. struct list_head *prev = p->mnt_mounts.prev;
  582. while (prev != &p->mnt_mounts) {
  583. p = list_entry(prev, struct vfsmount, mnt_child);
  584. prev = p->mnt_mounts.prev;
  585. }
  586. return p;
  587. }
  588. struct vfsmount *
  589. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  590. {
  591. struct vfsmount *mnt;
  592. struct dentry *root;
  593. if (!type)
  594. return ERR_PTR(-ENODEV);
  595. mnt = alloc_vfsmnt(name);
  596. if (!mnt)
  597. return ERR_PTR(-ENOMEM);
  598. if (flags & MS_KERNMOUNT)
  599. mnt->mnt_flags = MNT_INTERNAL;
  600. root = mount_fs(type, flags, name, data);
  601. if (IS_ERR(root)) {
  602. free_vfsmnt(mnt);
  603. return ERR_CAST(root);
  604. }
  605. mnt->mnt_root = root;
  606. mnt->mnt_sb = root->d_sb;
  607. mnt->mnt_mountpoint = mnt->mnt_root;
  608. mnt->mnt_parent = mnt;
  609. return mnt;
  610. }
  611. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  612. static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
  613. int flag)
  614. {
  615. struct super_block *sb = old->mnt_sb;
  616. struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
  617. if (mnt) {
  618. if (flag & (CL_SLAVE | CL_PRIVATE))
  619. mnt->mnt_group_id = 0; /* not a peer of original */
  620. else
  621. mnt->mnt_group_id = old->mnt_group_id;
  622. if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
  623. int err = mnt_alloc_group_id(mnt);
  624. if (err)
  625. goto out_free;
  626. }
  627. mnt->mnt_flags = old->mnt_flags & ~MNT_WRITE_HOLD;
  628. atomic_inc(&sb->s_active);
  629. mnt->mnt_sb = sb;
  630. mnt->mnt_root = dget(root);
  631. mnt->mnt_mountpoint = mnt->mnt_root;
  632. mnt->mnt_parent = mnt;
  633. if (flag & CL_SLAVE) {
  634. list_add(&mnt->mnt_slave, &old->mnt_slave_list);
  635. mnt->mnt_master = old;
  636. CLEAR_MNT_SHARED(mnt);
  637. } else if (!(flag & CL_PRIVATE)) {
  638. if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
  639. list_add(&mnt->mnt_share, &old->mnt_share);
  640. if (IS_MNT_SLAVE(old))
  641. list_add(&mnt->mnt_slave, &old->mnt_slave);
  642. mnt->mnt_master = old->mnt_master;
  643. }
  644. if (flag & CL_MAKE_SHARED)
  645. set_mnt_shared(mnt);
  646. /* stick the duplicate mount on the same expiry list
  647. * as the original if that was on one */
  648. if (flag & CL_EXPIRE) {
  649. if (!list_empty(&old->mnt_expire))
  650. list_add(&mnt->mnt_expire, &old->mnt_expire);
  651. }
  652. }
  653. return mnt;
  654. out_free:
  655. free_vfsmnt(mnt);
  656. return NULL;
  657. }
  658. static inline void mntfree(struct vfsmount *mnt)
  659. {
  660. struct super_block *sb = mnt->mnt_sb;
  661. /*
  662. * This probably indicates that somebody messed
  663. * up a mnt_want/drop_write() pair. If this
  664. * happens, the filesystem was probably unable
  665. * to make r/w->r/o transitions.
  666. */
  667. /*
  668. * The locking used to deal with mnt_count decrement provides barriers,
  669. * so mnt_get_writers() below is safe.
  670. */
  671. WARN_ON(mnt_get_writers(mnt));
  672. fsnotify_vfsmount_delete(mnt);
  673. dput(mnt->mnt_root);
  674. free_vfsmnt(mnt);
  675. deactivate_super(sb);
  676. }
  677. static void mntput_no_expire(struct vfsmount *mnt)
  678. {
  679. put_again:
  680. #ifdef CONFIG_SMP
  681. br_read_lock(vfsmount_lock);
  682. if (likely(atomic_read(&mnt->mnt_longterm))) {
  683. mnt_add_count(mnt, -1);
  684. br_read_unlock(vfsmount_lock);
  685. return;
  686. }
  687. br_read_unlock(vfsmount_lock);
  688. br_write_lock(vfsmount_lock);
  689. mnt_add_count(mnt, -1);
  690. if (mnt_get_count(mnt)) {
  691. br_write_unlock(vfsmount_lock);
  692. return;
  693. }
  694. #else
  695. mnt_add_count(mnt, -1);
  696. if (likely(mnt_get_count(mnt)))
  697. return;
  698. br_write_lock(vfsmount_lock);
  699. #endif
  700. if (unlikely(mnt->mnt_pinned)) {
  701. mnt_add_count(mnt, mnt->mnt_pinned + 1);
  702. mnt->mnt_pinned = 0;
  703. br_write_unlock(vfsmount_lock);
  704. acct_auto_close_mnt(mnt);
  705. goto put_again;
  706. }
  707. br_write_unlock(vfsmount_lock);
  708. mntfree(mnt);
  709. }
  710. void mntput(struct vfsmount *mnt)
  711. {
  712. if (mnt) {
  713. /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
  714. if (unlikely(mnt->mnt_expiry_mark))
  715. mnt->mnt_expiry_mark = 0;
  716. mntput_no_expire(mnt);
  717. }
  718. }
  719. EXPORT_SYMBOL(mntput);
  720. struct vfsmount *mntget(struct vfsmount *mnt)
  721. {
  722. if (mnt)
  723. mnt_add_count(mnt, 1);
  724. return mnt;
  725. }
  726. EXPORT_SYMBOL(mntget);
  727. void mnt_pin(struct vfsmount *mnt)
  728. {
  729. br_write_lock(vfsmount_lock);
  730. mnt->mnt_pinned++;
  731. br_write_unlock(vfsmount_lock);
  732. }
  733. EXPORT_SYMBOL(mnt_pin);
  734. void mnt_unpin(struct vfsmount *mnt)
  735. {
  736. br_write_lock(vfsmount_lock);
  737. if (mnt->mnt_pinned) {
  738. mnt_add_count(mnt, 1);
  739. mnt->mnt_pinned--;
  740. }
  741. br_write_unlock(vfsmount_lock);
  742. }
  743. EXPORT_SYMBOL(mnt_unpin);
  744. static inline void mangle(struct seq_file *m, const char *s)
  745. {
  746. seq_escape(m, s, " \t\n\\");
  747. }
  748. /*
  749. * Simple .show_options callback for filesystems which don't want to
  750. * implement more complex mount option showing.
  751. *
  752. * See also save_mount_options().
  753. */
  754. int generic_show_options(struct seq_file *m, struct vfsmount *mnt)
  755. {
  756. const char *options;
  757. rcu_read_lock();
  758. options = rcu_dereference(mnt->mnt_sb->s_options);
  759. if (options != NULL && options[0]) {
  760. seq_putc(m, ',');
  761. mangle(m, options);
  762. }
  763. rcu_read_unlock();
  764. return 0;
  765. }
  766. EXPORT_SYMBOL(generic_show_options);
  767. /*
  768. * If filesystem uses generic_show_options(), this function should be
  769. * called from the fill_super() callback.
  770. *
  771. * The .remount_fs callback usually needs to be handled in a special
  772. * way, to make sure, that previous options are not overwritten if the
  773. * remount fails.
  774. *
  775. * Also note, that if the filesystem's .remount_fs function doesn't
  776. * reset all options to their default value, but changes only newly
  777. * given options, then the displayed options will not reflect reality
  778. * any more.
  779. */
  780. void save_mount_options(struct super_block *sb, char *options)
  781. {
  782. BUG_ON(sb->s_options);
  783. rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
  784. }
  785. EXPORT_SYMBOL(save_mount_options);
  786. void replace_mount_options(struct super_block *sb, char *options)
  787. {
  788. char *old = sb->s_options;
  789. rcu_assign_pointer(sb->s_options, options);
  790. if (old) {
  791. synchronize_rcu();
  792. kfree(old);
  793. }
  794. }
  795. EXPORT_SYMBOL(replace_mount_options);
  796. #ifdef CONFIG_PROC_FS
  797. /* iterator */
  798. static void *m_start(struct seq_file *m, loff_t *pos)
  799. {
  800. struct proc_mounts *p = m->private;
  801. down_read(&namespace_sem);
  802. return seq_list_start(&p->ns->list, *pos);
  803. }
  804. static void *m_next(struct seq_file *m, void *v, loff_t *pos)
  805. {
  806. struct proc_mounts *p = m->private;
  807. return seq_list_next(v, &p->ns->list, pos);
  808. }
  809. static void m_stop(struct seq_file *m, void *v)
  810. {
  811. up_read(&namespace_sem);
  812. }
  813. int mnt_had_events(struct proc_mounts *p)
  814. {
  815. struct mnt_namespace *ns = p->ns;
  816. int res = 0;
  817. br_read_lock(vfsmount_lock);
  818. if (p->m.poll_event != ns->event) {
  819. p->m.poll_event = ns->event;
  820. res = 1;
  821. }
  822. br_read_unlock(vfsmount_lock);
  823. return res;
  824. }
  825. struct proc_fs_info {
  826. int flag;
  827. const char *str;
  828. };
  829. static int show_sb_opts(struct seq_file *m, struct super_block *sb)
  830. {
  831. static const struct proc_fs_info fs_info[] = {
  832. { MS_SYNCHRONOUS, ",sync" },
  833. { MS_DIRSYNC, ",dirsync" },
  834. { MS_MANDLOCK, ",mand" },
  835. { 0, NULL }
  836. };
  837. const struct proc_fs_info *fs_infop;
  838. for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
  839. if (sb->s_flags & fs_infop->flag)
  840. seq_puts(m, fs_infop->str);
  841. }
  842. return security_sb_show_options(m, sb);
  843. }
  844. static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt)
  845. {
  846. static const struct proc_fs_info mnt_info[] = {
  847. { MNT_NOSUID, ",nosuid" },
  848. { MNT_NODEV, ",nodev" },
  849. { MNT_NOEXEC, ",noexec" },
  850. { MNT_NOATIME, ",noatime" },
  851. { MNT_NODIRATIME, ",nodiratime" },
  852. { MNT_RELATIME, ",relatime" },
  853. { 0, NULL }
  854. };
  855. const struct proc_fs_info *fs_infop;
  856. for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
  857. if (mnt->mnt_flags & fs_infop->flag)
  858. seq_puts(m, fs_infop->str);
  859. }
  860. }
  861. static void show_type(struct seq_file *m, struct super_block *sb)
  862. {
  863. mangle(m, sb->s_type->name);
  864. if (sb->s_subtype && sb->s_subtype[0]) {
  865. seq_putc(m, '.');
  866. mangle(m, sb->s_subtype);
  867. }
  868. }
  869. static int show_vfsmnt(struct seq_file *m, void *v)
  870. {
  871. struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
  872. int err = 0;
  873. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  874. if (mnt->mnt_sb->s_op->show_devname) {
  875. err = mnt->mnt_sb->s_op->show_devname(m, mnt);
  876. if (err)
  877. goto out;
  878. } else {
  879. mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
  880. }
  881. seq_putc(m, ' ');
  882. seq_path(m, &mnt_path, " \t\n\\");
  883. seq_putc(m, ' ');
  884. show_type(m, mnt->mnt_sb);
  885. seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw");
  886. err = show_sb_opts(m, mnt->mnt_sb);
  887. if (err)
  888. goto out;
  889. show_mnt_opts(m, mnt);
  890. if (mnt->mnt_sb->s_op->show_options)
  891. err = mnt->mnt_sb->s_op->show_options(m, mnt);
  892. seq_puts(m, " 0 0\n");
  893. out:
  894. return err;
  895. }
  896. const struct seq_operations mounts_op = {
  897. .start = m_start,
  898. .next = m_next,
  899. .stop = m_stop,
  900. .show = show_vfsmnt
  901. };
  902. static int show_mountinfo(struct seq_file *m, void *v)
  903. {
  904. struct proc_mounts *p = m->private;
  905. struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
  906. struct super_block *sb = mnt->mnt_sb;
  907. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  908. struct path root = p->root;
  909. int err = 0;
  910. seq_printf(m, "%i %i %u:%u ", mnt->mnt_id, mnt->mnt_parent->mnt_id,
  911. MAJOR(sb->s_dev), MINOR(sb->s_dev));
  912. if (sb->s_op->show_path)
  913. err = sb->s_op->show_path(m, mnt);
  914. else
  915. seq_dentry(m, mnt->mnt_root, " \t\n\\");
  916. if (err)
  917. goto out;
  918. seq_putc(m, ' ');
  919. /* mountpoints outside of chroot jail will give SEQ_SKIP on this */
  920. err = seq_path_root(m, &mnt_path, &root, " \t\n\\");
  921. if (err)
  922. goto out;
  923. seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw");
  924. show_mnt_opts(m, mnt);
  925. /* Tagged fields ("foo:X" or "bar") */
  926. if (IS_MNT_SHARED(mnt))
  927. seq_printf(m, " shared:%i", mnt->mnt_group_id);
  928. if (IS_MNT_SLAVE(mnt)) {
  929. int master = mnt->mnt_master->mnt_group_id;
  930. int dom = get_dominating_id(mnt, &p->root);
  931. seq_printf(m, " master:%i", master);
  932. if (dom && dom != master)
  933. seq_printf(m, " propagate_from:%i", dom);
  934. }
  935. if (IS_MNT_UNBINDABLE(mnt))
  936. seq_puts(m, " unbindable");
  937. /* Filesystem specific data */
  938. seq_puts(m, " - ");
  939. show_type(m, sb);
  940. seq_putc(m, ' ');
  941. if (sb->s_op->show_devname)
  942. err = sb->s_op->show_devname(m, mnt);
  943. else
  944. mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
  945. if (err)
  946. goto out;
  947. seq_puts(m, sb->s_flags & MS_RDONLY ? " ro" : " rw");
  948. err = show_sb_opts(m, sb);
  949. if (err)
  950. goto out;
  951. if (sb->s_op->show_options)
  952. err = sb->s_op->show_options(m, mnt);
  953. seq_putc(m, '\n');
  954. out:
  955. return err;
  956. }
  957. const struct seq_operations mountinfo_op = {
  958. .start = m_start,
  959. .next = m_next,
  960. .stop = m_stop,
  961. .show = show_mountinfo,
  962. };
  963. static int show_vfsstat(struct seq_file *m, void *v)
  964. {
  965. struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
  966. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  967. int err = 0;
  968. /* device */
  969. if (mnt->mnt_sb->s_op->show_devname) {
  970. seq_puts(m, "device ");
  971. err = mnt->mnt_sb->s_op->show_devname(m, mnt);
  972. } else {
  973. if (mnt->mnt_devname) {
  974. seq_puts(m, "device ");
  975. mangle(m, mnt->mnt_devname);
  976. } else
  977. seq_puts(m, "no device");
  978. }
  979. /* mount point */
  980. seq_puts(m, " mounted on ");
  981. seq_path(m, &mnt_path, " \t\n\\");
  982. seq_putc(m, ' ');
  983. /* file system type */
  984. seq_puts(m, "with fstype ");
  985. show_type(m, mnt->mnt_sb);
  986. /* optional statistics */
  987. if (mnt->mnt_sb->s_op->show_stats) {
  988. seq_putc(m, ' ');
  989. if (!err)
  990. err = mnt->mnt_sb->s_op->show_stats(m, mnt);
  991. }
  992. seq_putc(m, '\n');
  993. return err;
  994. }
  995. const struct seq_operations mountstats_op = {
  996. .start = m_start,
  997. .next = m_next,
  998. .stop = m_stop,
  999. .show = show_vfsstat,
  1000. };
  1001. #endif /* CONFIG_PROC_FS */
  1002. /**
  1003. * may_umount_tree - check if a mount tree is busy
  1004. * @mnt: root of mount tree
  1005. *
  1006. * This is called to check if a tree of mounts has any
  1007. * open files, pwds, chroots or sub mounts that are
  1008. * busy.
  1009. */
  1010. int may_umount_tree(struct vfsmount *mnt)
  1011. {
  1012. int actual_refs = 0;
  1013. int minimum_refs = 0;
  1014. struct vfsmount *p;
  1015. /* write lock needed for mnt_get_count */
  1016. br_write_lock(vfsmount_lock);
  1017. for (p = mnt; p; p = next_mnt(p, mnt)) {
  1018. actual_refs += mnt_get_count(p);
  1019. minimum_refs += 2;
  1020. }
  1021. br_write_unlock(vfsmount_lock);
  1022. if (actual_refs > minimum_refs)
  1023. return 0;
  1024. return 1;
  1025. }
  1026. EXPORT_SYMBOL(may_umount_tree);
  1027. /**
  1028. * may_umount - check if a mount point is busy
  1029. * @mnt: root of mount
  1030. *
  1031. * This is called to check if a mount point has any
  1032. * open files, pwds, chroots or sub mounts. If the
  1033. * mount has sub mounts this will return busy
  1034. * regardless of whether the sub mounts are busy.
  1035. *
  1036. * Doesn't take quota and stuff into account. IOW, in some cases it will
  1037. * give false negatives. The main reason why it's here is that we need
  1038. * a non-destructive way to look for easily umountable filesystems.
  1039. */
  1040. int may_umount(struct vfsmount *mnt)
  1041. {
  1042. int ret = 1;
  1043. down_read(&namespace_sem);
  1044. br_write_lock(vfsmount_lock);
  1045. if (propagate_mount_busy(mnt, 2))
  1046. ret = 0;
  1047. br_write_unlock(vfsmount_lock);
  1048. up_read(&namespace_sem);
  1049. return ret;
  1050. }
  1051. EXPORT_SYMBOL(may_umount);
  1052. void release_mounts(struct list_head *head)
  1053. {
  1054. struct vfsmount *mnt;
  1055. while (!list_empty(head)) {
  1056. mnt = list_first_entry(head, struct vfsmount, mnt_hash);
  1057. list_del_init(&mnt->mnt_hash);
  1058. if (mnt_has_parent(mnt)) {
  1059. struct dentry *dentry;
  1060. struct vfsmount *m;
  1061. br_write_lock(vfsmount_lock);
  1062. dentry = mnt->mnt_mountpoint;
  1063. m = mnt->mnt_parent;
  1064. mnt->mnt_mountpoint = mnt->mnt_root;
  1065. mnt->mnt_parent = mnt;
  1066. m->mnt_ghosts--;
  1067. br_write_unlock(vfsmount_lock);
  1068. dput(dentry);
  1069. mntput(m);
  1070. }
  1071. mntput(mnt);
  1072. }
  1073. }
  1074. /*
  1075. * vfsmount lock must be held for write
  1076. * namespace_sem must be held for write
  1077. */
  1078. void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
  1079. {
  1080. LIST_HEAD(tmp_list);
  1081. struct vfsmount *p;
  1082. for (p = mnt; p; p = next_mnt(p, mnt))
  1083. list_move(&p->mnt_hash, &tmp_list);
  1084. if (propagate)
  1085. propagate_umount(&tmp_list);
  1086. list_for_each_entry(p, &tmp_list, mnt_hash) {
  1087. list_del_init(&p->mnt_expire);
  1088. list_del_init(&p->mnt_list);
  1089. __touch_mnt_namespace(p->mnt_ns);
  1090. p->mnt_ns = NULL;
  1091. __mnt_make_shortterm(p);
  1092. list_del_init(&p->mnt_child);
  1093. if (mnt_has_parent(p)) {
  1094. p->mnt_parent->mnt_ghosts++;
  1095. dentry_reset_mounted(p->mnt_mountpoint);
  1096. }
  1097. change_mnt_propagation(p, MS_PRIVATE);
  1098. }
  1099. list_splice(&tmp_list, kill);
  1100. }
  1101. static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts);
  1102. static int do_umount(struct vfsmount *mnt, int flags)
  1103. {
  1104. struct super_block *sb = mnt->mnt_sb;
  1105. int retval;
  1106. LIST_HEAD(umount_list);
  1107. retval = security_sb_umount(mnt, flags);
  1108. if (retval)
  1109. return retval;
  1110. /*
  1111. * Allow userspace to request a mountpoint be expired rather than
  1112. * unmounting unconditionally. Unmount only happens if:
  1113. * (1) the mark is already set (the mark is cleared by mntput())
  1114. * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
  1115. */
  1116. if (flags & MNT_EXPIRE) {
  1117. if (mnt == current->fs->root.mnt ||
  1118. flags & (MNT_FORCE | MNT_DETACH))
  1119. return -EINVAL;
  1120. /*
  1121. * probably don't strictly need the lock here if we examined
  1122. * all race cases, but it's a slowpath.
  1123. */
  1124. br_write_lock(vfsmount_lock);
  1125. if (mnt_get_count(mnt) != 2) {
  1126. br_write_unlock(vfsmount_lock);
  1127. return -EBUSY;
  1128. }
  1129. br_write_unlock(vfsmount_lock);
  1130. if (!xchg(&mnt->mnt_expiry_mark, 1))
  1131. return -EAGAIN;
  1132. }
  1133. /*
  1134. * If we may have to abort operations to get out of this
  1135. * mount, and they will themselves hold resources we must
  1136. * allow the fs to do things. In the Unix tradition of
  1137. * 'Gee thats tricky lets do it in userspace' the umount_begin
  1138. * might fail to complete on the first run through as other tasks
  1139. * must return, and the like. Thats for the mount program to worry
  1140. * about for the moment.
  1141. */
  1142. if (flags & MNT_FORCE && sb->s_op->umount_begin) {
  1143. sb->s_op->umount_begin(sb);
  1144. }
  1145. /*
  1146. * No sense to grab the lock for this test, but test itself looks
  1147. * somewhat bogus. Suggestions for better replacement?
  1148. * Ho-hum... In principle, we might treat that as umount + switch
  1149. * to rootfs. GC would eventually take care of the old vfsmount.
  1150. * Actually it makes sense, especially if rootfs would contain a
  1151. * /reboot - static binary that would close all descriptors and
  1152. * call reboot(9). Then init(8) could umount root and exec /reboot.
  1153. */
  1154. if (mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
  1155. /*
  1156. * Special case for "unmounting" root ...
  1157. * we just try to remount it readonly.
  1158. */
  1159. down_write(&sb->s_umount);
  1160. if (!(sb->s_flags & MS_RDONLY))
  1161. retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
  1162. up_write(&sb->s_umount);
  1163. return retval;
  1164. }
  1165. down_write(&namespace_sem);
  1166. br_write_lock(vfsmount_lock);
  1167. event++;
  1168. if (!(flags & MNT_DETACH))
  1169. shrink_submounts(mnt, &umount_list);
  1170. retval = -EBUSY;
  1171. if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
  1172. if (!list_empty(&mnt->mnt_list))
  1173. umount_tree(mnt, 1, &umount_list);
  1174. retval = 0;
  1175. }
  1176. br_write_unlock(vfsmount_lock);
  1177. up_write(&namespace_sem);
  1178. release_mounts(&umount_list);
  1179. return retval;
  1180. }
  1181. /*
  1182. * Now umount can handle mount points as well as block devices.
  1183. * This is important for filesystems which use unnamed block devices.
  1184. *
  1185. * We now support a flag for forced unmount like the other 'big iron'
  1186. * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
  1187. */
  1188. SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
  1189. {
  1190. struct path path;
  1191. int retval;
  1192. int lookup_flags = 0;
  1193. if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
  1194. return -EINVAL;
  1195. if (!(flags & UMOUNT_NOFOLLOW))
  1196. lookup_flags |= LOOKUP_FOLLOW;
  1197. retval = user_path_at(AT_FDCWD, name, lookup_flags, &path);
  1198. if (retval)
  1199. goto out;
  1200. retval = -EINVAL;
  1201. if (path.dentry != path.mnt->mnt_root)
  1202. goto dput_and_out;
  1203. if (!check_mnt(path.mnt))
  1204. goto dput_and_out;
  1205. retval = -EPERM;
  1206. if (!capable(CAP_SYS_ADMIN))
  1207. goto dput_and_out;
  1208. retval = do_umount(path.mnt, flags);
  1209. dput_and_out:
  1210. /* we mustn't call path_put() as that would clear mnt_expiry_mark */
  1211. dput(path.dentry);
  1212. mntput_no_expire(path.mnt);
  1213. out:
  1214. return retval;
  1215. }
  1216. #ifdef __ARCH_WANT_SYS_OLDUMOUNT
  1217. /*
  1218. * The 2.0 compatible umount. No flags.
  1219. */
  1220. SYSCALL_DEFINE1(oldumount, char __user *, name)
  1221. {
  1222. return sys_umount(name, 0);
  1223. }
  1224. #endif
  1225. static int mount_is_safe(struct path *path)
  1226. {
  1227. if (capable(CAP_SYS_ADMIN))
  1228. return 0;
  1229. return -EPERM;
  1230. #ifdef notyet
  1231. if (S_ISLNK(path->dentry->d_inode->i_mode))
  1232. return -EPERM;
  1233. if (path->dentry->d_inode->i_mode & S_ISVTX) {
  1234. if (current_uid() != path->dentry->d_inode->i_uid)
  1235. return -EPERM;
  1236. }
  1237. if (inode_permission(path->dentry->d_inode, MAY_WRITE))
  1238. return -EPERM;
  1239. return 0;
  1240. #endif
  1241. }
  1242. struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
  1243. int flag)
  1244. {
  1245. struct vfsmount *res, *p, *q, *r, *s;
  1246. struct path path;
  1247. if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
  1248. return NULL;
  1249. res = q = clone_mnt(mnt, dentry, flag);
  1250. if (!q)
  1251. goto Enomem;
  1252. q->mnt_mountpoint = mnt->mnt_mountpoint;
  1253. p = mnt;
  1254. list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
  1255. if (!is_subdir(r->mnt_mountpoint, dentry))
  1256. continue;
  1257. for (s = r; s; s = next_mnt(s, r)) {
  1258. if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
  1259. s = skip_mnt_tree(s);
  1260. continue;
  1261. }
  1262. while (p != s->mnt_parent) {
  1263. p = p->mnt_parent;
  1264. q = q->mnt_parent;
  1265. }
  1266. p = s;
  1267. path.mnt = q;
  1268. path.dentry = p->mnt_mountpoint;
  1269. q = clone_mnt(p, p->mnt_root, flag);
  1270. if (!q)
  1271. goto Enomem;
  1272. br_write_lock(vfsmount_lock);
  1273. list_add_tail(&q->mnt_list, &res->mnt_list);
  1274. attach_mnt(q, &path);
  1275. br_write_unlock(vfsmount_lock);
  1276. }
  1277. }
  1278. return res;
  1279. Enomem:
  1280. if (res) {
  1281. LIST_HEAD(umount_list);
  1282. br_write_lock(vfsmount_lock);
  1283. umount_tree(res, 0, &umount_list);
  1284. br_write_unlock(vfsmount_lock);
  1285. release_mounts(&umount_list);
  1286. }
  1287. return NULL;
  1288. }
  1289. struct vfsmount *collect_mounts(struct path *path)
  1290. {
  1291. struct vfsmount *tree;
  1292. down_write(&namespace_sem);
  1293. tree = copy_tree(path->mnt, path->dentry, CL_COPY_ALL | CL_PRIVATE);
  1294. up_write(&namespace_sem);
  1295. return tree;
  1296. }
  1297. void drop_collected_mounts(struct vfsmount *mnt)
  1298. {
  1299. LIST_HEAD(umount_list);
  1300. down_write(&namespace_sem);
  1301. br_write_lock(vfsmount_lock);
  1302. umount_tree(mnt, 0, &umount_list);
  1303. br_write_unlock(vfsmount_lock);
  1304. up_write(&namespace_sem);
  1305. release_mounts(&umount_list);
  1306. }
  1307. int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
  1308. struct vfsmount *root)
  1309. {
  1310. struct vfsmount *mnt;
  1311. int res = f(root, arg);
  1312. if (res)
  1313. return res;
  1314. list_for_each_entry(mnt, &root->mnt_list, mnt_list) {
  1315. res = f(mnt, arg);
  1316. if (res)
  1317. return res;
  1318. }
  1319. return 0;
  1320. }
  1321. static void cleanup_group_ids(struct vfsmount *mnt, struct vfsmount *end)
  1322. {
  1323. struct vfsmount *p;
  1324. for (p = mnt; p != end; p = next_mnt(p, mnt)) {
  1325. if (p->mnt_group_id && !IS_MNT_SHARED(p))
  1326. mnt_release_group_id(p);
  1327. }
  1328. }
  1329. static int invent_group_ids(struct vfsmount *mnt, bool recurse)
  1330. {
  1331. struct vfsmount *p;
  1332. for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
  1333. if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
  1334. int err = mnt_alloc_group_id(p);
  1335. if (err) {
  1336. cleanup_group_ids(mnt, p);
  1337. return err;
  1338. }
  1339. }
  1340. }
  1341. return 0;
  1342. }
  1343. /*
  1344. * @source_mnt : mount tree to be attached
  1345. * @nd : place the mount tree @source_mnt is attached
  1346. * @parent_nd : if non-null, detach the source_mnt from its parent and
  1347. * store the parent mount and mountpoint dentry.
  1348. * (done when source_mnt is moved)
  1349. *
  1350. * NOTE: in the table below explains the semantics when a source mount
  1351. * of a given type is attached to a destination mount of a given type.
  1352. * ---------------------------------------------------------------------------
  1353. * | BIND MOUNT OPERATION |
  1354. * |**************************************************************************
  1355. * | source-->| shared | private | slave | unbindable |
  1356. * | dest | | | | |
  1357. * | | | | | | |
  1358. * | v | | | | |
  1359. * |**************************************************************************
  1360. * | shared | shared (++) | shared (+) | shared(+++)| invalid |
  1361. * | | | | | |
  1362. * |non-shared| shared (+) | private | slave (*) | invalid |
  1363. * ***************************************************************************
  1364. * A bind operation clones the source mount and mounts the clone on the
  1365. * destination mount.
  1366. *
  1367. * (++) the cloned mount is propagated to all the mounts in the propagation
  1368. * tree of the destination mount and the cloned mount is added to
  1369. * the peer group of the source mount.
  1370. * (+) the cloned mount is created under the destination mount and is marked
  1371. * as shared. The cloned mount is added to the peer group of the source
  1372. * mount.
  1373. * (+++) the mount is propagated to all the mounts in the propagation tree
  1374. * of the destination mount and the cloned mount is made slave
  1375. * of the same master as that of the source mount. The cloned mount
  1376. * is marked as 'shared and slave'.
  1377. * (*) the cloned mount is made a slave of the same master as that of the
  1378. * source mount.
  1379. *
  1380. * ---------------------------------------------------------------------------
  1381. * | MOVE MOUNT OPERATION |
  1382. * |**************************************************************************
  1383. * | source-->| shared | private | slave | unbindable |
  1384. * | dest | | | | |
  1385. * | | | | | | |
  1386. * | v | | | | |
  1387. * |**************************************************************************
  1388. * | shared | shared (+) | shared (+) | shared(+++) | invalid |
  1389. * | | | | | |
  1390. * |non-shared| shared (+*) | private | slave (*) | unbindable |
  1391. * ***************************************************************************
  1392. *
  1393. * (+) the mount is moved to the destination. And is then propagated to
  1394. * all the mounts in the propagation tree of the destination mount.
  1395. * (+*) the mount is moved to the destination.
  1396. * (+++) the mount is moved to the destination and is then propagated to
  1397. * all the mounts belonging to the destination mount's propagation tree.
  1398. * the mount is marked as 'shared and slave'.
  1399. * (*) the mount continues to be a slave at the new location.
  1400. *
  1401. * if the source mount is a tree, the operations explained above is
  1402. * applied to each mount in the tree.
  1403. * Must be called without spinlocks held, since this function can sleep
  1404. * in allocations.
  1405. */
  1406. static int attach_recursive_mnt(struct vfsmount *source_mnt,
  1407. struct path *path, struct path *parent_path)
  1408. {
  1409. LIST_HEAD(tree_list);
  1410. struct vfsmount *dest_mnt = path->mnt;
  1411. struct dentry *dest_dentry = path->dentry;
  1412. struct vfsmount *child, *p;
  1413. int err;
  1414. if (IS_MNT_SHARED(dest_mnt)) {
  1415. err = invent_group_ids(source_mnt, true);
  1416. if (err)
  1417. goto out;
  1418. }
  1419. err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list);
  1420. if (err)
  1421. goto out_cleanup_ids;
  1422. br_write_lock(vfsmount_lock);
  1423. if (IS_MNT_SHARED(dest_mnt)) {
  1424. for (p = source_mnt; p; p = next_mnt(p, source_mnt))
  1425. set_mnt_shared(p);
  1426. }
  1427. if (parent_path) {
  1428. detach_mnt(source_mnt, parent_path);
  1429. attach_mnt(source_mnt, path);
  1430. touch_mnt_namespace(parent_path->mnt->mnt_ns);
  1431. } else {
  1432. mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
  1433. commit_tree(source_mnt);
  1434. }
  1435. list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
  1436. list_del_init(&child->mnt_hash);
  1437. commit_tree(child);
  1438. }
  1439. br_write_unlock(vfsmount_lock);
  1440. return 0;
  1441. out_cleanup_ids:
  1442. if (IS_MNT_SHARED(dest_mnt))
  1443. cleanup_group_ids(source_mnt, NULL);
  1444. out:
  1445. return err;
  1446. }
  1447. static int lock_mount(struct path *path)
  1448. {
  1449. struct vfsmount *mnt;
  1450. retry:
  1451. mutex_lock(&path->dentry->d_inode->i_mutex);
  1452. if (unlikely(cant_mount(path->dentry))) {
  1453. mutex_unlock(&path->dentry->d_inode->i_mutex);
  1454. return -ENOENT;
  1455. }
  1456. down_write(&namespace_sem);
  1457. mnt = lookup_mnt(path);
  1458. if (likely(!mnt))
  1459. return 0;
  1460. up_write(&namespace_sem);
  1461. mutex_unlock(&path->dentry->d_inode->i_mutex);
  1462. path_put(path);
  1463. path->mnt = mnt;
  1464. path->dentry = dget(mnt->mnt_root);
  1465. goto retry;
  1466. }
  1467. static void unlock_mount(struct path *path)
  1468. {
  1469. up_write(&namespace_sem);
  1470. mutex_unlock(&path->dentry->d_inode->i_mutex);
  1471. }
  1472. static int graft_tree(struct vfsmount *mnt, struct path *path)
  1473. {
  1474. if (mnt->mnt_sb->s_flags & MS_NOUSER)
  1475. return -EINVAL;
  1476. if (S_ISDIR(path->dentry->d_inode->i_mode) !=
  1477. S_ISDIR(mnt->mnt_root->d_inode->i_mode))
  1478. return -ENOTDIR;
  1479. if (d_unlinked(path->dentry))
  1480. return -ENOENT;
  1481. return attach_recursive_mnt(mnt, path, NULL);
  1482. }
  1483. /*
  1484. * Sanity check the flags to change_mnt_propagation.
  1485. */
  1486. static int flags_to_propagation_type(int flags)
  1487. {
  1488. int type = flags & ~(MS_REC | MS_SILENT);
  1489. /* Fail if any non-propagation flags are set */
  1490. if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  1491. return 0;
  1492. /* Only one propagation flag should be set */
  1493. if (!is_power_of_2(type))
  1494. return 0;
  1495. return type;
  1496. }
  1497. /*
  1498. * recursively change the type of the mountpoint.
  1499. */
  1500. static int do_change_type(struct path *path, int flag)
  1501. {
  1502. struct vfsmount *m, *mnt = path->mnt;
  1503. int recurse = flag & MS_REC;
  1504. int type;
  1505. int err = 0;
  1506. if (!capable(CAP_SYS_ADMIN))
  1507. return -EPERM;
  1508. if (path->dentry != path->mnt->mnt_root)
  1509. return -EINVAL;
  1510. type = flags_to_propagation_type(flag);
  1511. if (!type)
  1512. return -EINVAL;
  1513. down_write(&namespace_sem);
  1514. if (type == MS_SHARED) {
  1515. err = invent_group_ids(mnt, recurse);
  1516. if (err)
  1517. goto out_unlock;
  1518. }
  1519. br_write_lock(vfsmount_lock);
  1520. for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
  1521. change_mnt_propagation(m, type);
  1522. br_write_unlock(vfsmount_lock);
  1523. out_unlock:
  1524. up_write(&namespace_sem);
  1525. return err;
  1526. }
  1527. /*
  1528. * do loopback mount.
  1529. */
  1530. static int do_loopback(struct path *path, char *old_name,
  1531. int recurse)
  1532. {
  1533. LIST_HEAD(umount_list);
  1534. struct path old_path;
  1535. struct vfsmount *mnt = NULL;
  1536. int err = mount_is_safe(path);
  1537. if (err)
  1538. return err;
  1539. if (!old_name || !*old_name)
  1540. return -EINVAL;
  1541. err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
  1542. if (err)
  1543. return err;
  1544. err = lock_mount(path);
  1545. if (err)
  1546. goto out;
  1547. err = -EINVAL;
  1548. if (IS_MNT_UNBINDABLE(old_path.mnt))
  1549. goto out2;
  1550. if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt))
  1551. goto out2;
  1552. err = -ENOMEM;
  1553. if (recurse)
  1554. mnt = copy_tree(old_path.mnt, old_path.dentry, 0);
  1555. else
  1556. mnt = clone_mnt(old_path.mnt, old_path.dentry, 0);
  1557. if (!mnt)
  1558. goto out2;
  1559. err = graft_tree(mnt, path);
  1560. if (err) {
  1561. br_write_lock(vfsmount_lock);
  1562. umount_tree(mnt, 0, &umount_list);
  1563. br_write_unlock(vfsmount_lock);
  1564. }
  1565. out2:
  1566. unlock_mount(path);
  1567. release_mounts(&umount_list);
  1568. out:
  1569. path_put(&old_path);
  1570. return err;
  1571. }
  1572. static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
  1573. {
  1574. int error = 0;
  1575. int readonly_request = 0;
  1576. if (ms_flags & MS_RDONLY)
  1577. readonly_request = 1;
  1578. if (readonly_request == __mnt_is_readonly(mnt))
  1579. return 0;
  1580. if (readonly_request)
  1581. error = mnt_make_readonly(mnt);
  1582. else
  1583. __mnt_unmake_readonly(mnt);
  1584. return error;
  1585. }
  1586. /*
  1587. * change filesystem flags. dir should be a physical root of filesystem.
  1588. * If you've mounted a non-root directory somewhere and want to do remount
  1589. * on it - tough luck.
  1590. */
  1591. static int do_remount(struct path *path, int flags, int mnt_flags,
  1592. void *data)
  1593. {
  1594. int err;
  1595. struct super_block *sb = path->mnt->mnt_sb;
  1596. if (!capable(CAP_SYS_ADMIN))
  1597. return -EPERM;
  1598. if (!check_mnt(path->mnt))
  1599. return -EINVAL;
  1600. if (path->dentry != path->mnt->mnt_root)
  1601. return -EINVAL;
  1602. err = security_sb_remount(sb, data);
  1603. if (err)
  1604. return err;
  1605. down_write(&sb->s_umount);
  1606. if (flags & MS_BIND)
  1607. err = change_mount_flags(path->mnt, flags);
  1608. else
  1609. err = do_remount_sb(sb, flags, data, 0);
  1610. if (!err) {
  1611. br_write_lock(vfsmount_lock);
  1612. mnt_flags |= path->mnt->mnt_flags & MNT_PROPAGATION_MASK;
  1613. path->mnt->mnt_flags = mnt_flags;
  1614. br_write_unlock(vfsmount_lock);
  1615. }
  1616. up_write(&sb->s_umount);
  1617. if (!err) {
  1618. br_write_lock(vfsmount_lock);
  1619. touch_mnt_namespace(path->mnt->mnt_ns);
  1620. br_write_unlock(vfsmount_lock);
  1621. }
  1622. return err;
  1623. }
  1624. static inline int tree_contains_unbindable(struct vfsmount *mnt)
  1625. {
  1626. struct vfsmount *p;
  1627. for (p = mnt; p; p = next_mnt(p, mnt)) {
  1628. if (IS_MNT_UNBINDABLE(p))
  1629. return 1;
  1630. }
  1631. return 0;
  1632. }
  1633. static int do_move_mount(struct path *path, char *old_name)
  1634. {
  1635. struct path old_path, parent_path;
  1636. struct vfsmount *p;
  1637. int err = 0;
  1638. if (!capable(CAP_SYS_ADMIN))
  1639. return -EPERM;
  1640. if (!old_name || !*old_name)
  1641. return -EINVAL;
  1642. err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
  1643. if (err)
  1644. return err;
  1645. err = lock_mount(path);
  1646. if (err < 0)
  1647. goto out;
  1648. err = -EINVAL;
  1649. if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt))
  1650. goto out1;
  1651. if (d_unlinked(path->dentry))
  1652. goto out1;
  1653. err = -EINVAL;
  1654. if (old_path.dentry != old_path.mnt->mnt_root)
  1655. goto out1;
  1656. if (!mnt_has_parent(old_path.mnt))
  1657. goto out1;
  1658. if (S_ISDIR(path->dentry->d_inode->i_mode) !=
  1659. S_ISDIR(old_path.dentry->d_inode->i_mode))
  1660. goto out1;
  1661. /*
  1662. * Don't move a mount residing in a shared parent.
  1663. */
  1664. if (IS_MNT_SHARED(old_path.mnt->mnt_parent))
  1665. goto out1;
  1666. /*
  1667. * Don't move a mount tree containing unbindable mounts to a destination
  1668. * mount which is shared.
  1669. */
  1670. if (IS_MNT_SHARED(path->mnt) &&
  1671. tree_contains_unbindable(old_path.mnt))
  1672. goto out1;
  1673. err = -ELOOP;
  1674. for (p = path->mnt; mnt_has_parent(p); p = p->mnt_parent)
  1675. if (p == old_path.mnt)
  1676. goto out1;
  1677. err = attach_recursive_mnt(old_path.mnt, path, &parent_path);
  1678. if (err)
  1679. goto out1;
  1680. /* if the mount is moved, it should no longer be expire
  1681. * automatically */
  1682. list_del_init(&old_path.mnt->mnt_expire);
  1683. out1:
  1684. unlock_mount(path);
  1685. out:
  1686. if (!err)
  1687. path_put(&parent_path);
  1688. path_put(&old_path);
  1689. return err;
  1690. }
  1691. static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
  1692. {
  1693. int err;
  1694. const char *subtype = strchr(fstype, '.');
  1695. if (subtype) {
  1696. subtype++;
  1697. err = -EINVAL;
  1698. if (!subtype[0])
  1699. goto err;
  1700. } else
  1701. subtype = "";
  1702. mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
  1703. err = -ENOMEM;
  1704. if (!mnt->mnt_sb->s_subtype)
  1705. goto err;
  1706. return mnt;
  1707. err:
  1708. mntput(mnt);
  1709. return ERR_PTR(err);
  1710. }
  1711. static struct vfsmount *
  1712. do_kern_mount(const char *fstype, int flags, const char *name, void *data)
  1713. {
  1714. struct file_system_type *type = get_fs_type(fstype);
  1715. struct vfsmount *mnt;
  1716. if (!type)
  1717. return ERR_PTR(-ENODEV);
  1718. mnt = vfs_kern_mount(type, flags, name, data);
  1719. if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
  1720. !mnt->mnt_sb->s_subtype)
  1721. mnt = fs_set_subtype(mnt, fstype);
  1722. put_filesystem(type);
  1723. return mnt;
  1724. }
  1725. /*
  1726. * add a mount into a namespace's mount tree
  1727. */
  1728. static int do_add_mount(struct vfsmount *newmnt, struct path *path, int mnt_flags)
  1729. {
  1730. int err;
  1731. mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
  1732. err = lock_mount(path);
  1733. if (err)
  1734. return err;
  1735. err = -EINVAL;
  1736. if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(path->mnt))
  1737. goto unlock;
  1738. /* Refuse the same filesystem on the same mount point */
  1739. err = -EBUSY;
  1740. if (path->mnt->mnt_sb == newmnt->mnt_sb &&
  1741. path->mnt->mnt_root == path->dentry)
  1742. goto unlock;
  1743. err = -EINVAL;
  1744. if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
  1745. goto unlock;
  1746. newmnt->mnt_flags = mnt_flags;
  1747. err = graft_tree(newmnt, path);
  1748. unlock:
  1749. unlock_mount(path);
  1750. return err;
  1751. }
  1752. /*
  1753. * create a new mount for userspace and request it to be added into the
  1754. * namespace's tree
  1755. */
  1756. static int do_new_mount(struct path *path, char *type, int flags,
  1757. int mnt_flags, char *name, void *data)
  1758. {
  1759. struct vfsmount *mnt;
  1760. int err;
  1761. if (!type)
  1762. return -EINVAL;
  1763. /* we need capabilities... */
  1764. if (!capable(CAP_SYS_ADMIN))
  1765. return -EPERM;
  1766. mnt = do_kern_mount(type, flags, name, data);
  1767. if (IS_ERR(mnt))
  1768. return PTR_ERR(mnt);
  1769. err = do_add_mount(mnt, path, mnt_flags);
  1770. if (err)
  1771. mntput(mnt);
  1772. return err;
  1773. }
  1774. int finish_automount(struct vfsmount *m, struct path *path)
  1775. {
  1776. int err;
  1777. /* The new mount record should have at least 2 refs to prevent it being
  1778. * expired before we get a chance to add it
  1779. */
  1780. BUG_ON(mnt_get_count(m) < 2);
  1781. if (m->mnt_sb == path->mnt->mnt_sb &&
  1782. m->mnt_root == path->dentry) {
  1783. err = -ELOOP;
  1784. goto fail;
  1785. }
  1786. err = do_add_mount(m, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
  1787. if (!err)
  1788. return 0;
  1789. fail:
  1790. /* remove m from any expiration list it may be on */
  1791. if (!list_empty(&m->mnt_expire)) {
  1792. down_write(&namespace_sem);
  1793. br_write_lock(vfsmount_lock);
  1794. list_del_init(&m->mnt_expire);
  1795. br_write_unlock(vfsmount_lock);
  1796. up_write(&namespace_sem);
  1797. }
  1798. mntput(m);
  1799. mntput(m);
  1800. return err;
  1801. }
  1802. /**
  1803. * mnt_set_expiry - Put a mount on an expiration list
  1804. * @mnt: The mount to list.
  1805. * @expiry_list: The list to add the mount to.
  1806. */
  1807. void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
  1808. {
  1809. down_write(&namespace_sem);
  1810. br_write_lock(vfsmount_lock);
  1811. list_add_tail(&mnt->mnt_expire, expiry_list);
  1812. br_write_unlock(vfsmount_lock);
  1813. up_write(&namespace_sem);
  1814. }
  1815. EXPORT_SYMBOL(mnt_set_expiry);
  1816. /*
  1817. * process a list of expirable mountpoints with the intent of discarding any
  1818. * mountpoints that aren't in use and haven't been touched since last we came
  1819. * here
  1820. */
  1821. void mark_mounts_for_expiry(struct list_head *mounts)
  1822. {
  1823. struct vfsmount *mnt, *next;
  1824. LIST_HEAD(graveyard);
  1825. LIST_HEAD(umounts);
  1826. if (list_empty(mounts))
  1827. return;
  1828. down_write(&namespace_sem);
  1829. br_write_lock(vfsmount_lock);
  1830. /* extract from the expiration list every vfsmount that matches the
  1831. * following criteria:
  1832. * - only referenced by its parent vfsmount
  1833. * - still marked for expiry (marked on the last call here; marks are
  1834. * cleared by mntput())
  1835. */
  1836. list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
  1837. if (!xchg(&mnt->mnt_expiry_mark, 1) ||
  1838. propagate_mount_busy(mnt, 1))
  1839. continue;
  1840. list_move(&mnt->mnt_expire, &graveyard);
  1841. }
  1842. while (!list_empty(&graveyard)) {
  1843. mnt = list_first_entry(&graveyard, struct vfsmount, mnt_expire);
  1844. touch_mnt_namespace(mnt->mnt_ns);
  1845. umount_tree(mnt, 1, &umounts);
  1846. }
  1847. br_write_unlock(vfsmount_lock);
  1848. up_write(&namespace_sem);
  1849. release_mounts(&umounts);
  1850. }
  1851. EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
  1852. /*
  1853. * Ripoff of 'select_parent()'
  1854. *
  1855. * search the list of submounts for a given mountpoint, and move any
  1856. * shrinkable submounts to the 'graveyard' list.
  1857. */
  1858. static int select_submounts(struct vfsmount *parent, struct list_head *graveyard)
  1859. {
  1860. struct vfsmount *this_parent = parent;
  1861. struct list_head *next;
  1862. int found = 0;
  1863. repeat:
  1864. next = this_parent->mnt_mounts.next;
  1865. resume:
  1866. while (next != &this_parent->mnt_mounts) {
  1867. struct list_head *tmp = next;
  1868. struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child);
  1869. next = tmp->next;
  1870. if (!(mnt->mnt_flags & MNT_SHRINKABLE))
  1871. continue;
  1872. /*
  1873. * Descend a level if the d_mounts list is non-empty.
  1874. */
  1875. if (!list_empty(&mnt->mnt_mounts)) {
  1876. this_parent = mnt;
  1877. goto repeat;
  1878. }
  1879. if (!propagate_mount_busy(mnt, 1)) {
  1880. list_move_tail(&mnt->mnt_expire, graveyard);
  1881. found++;
  1882. }
  1883. }
  1884. /*
  1885. * All done at this level ... ascend and resume the search
  1886. */
  1887. if (this_parent != parent) {
  1888. next = this_parent->mnt_child.next;
  1889. this_parent = this_parent->mnt_parent;
  1890. goto resume;
  1891. }
  1892. return found;
  1893. }
  1894. /*
  1895. * process a list of expirable mountpoints with the intent of discarding any
  1896. * submounts of a specific parent mountpoint
  1897. *
  1898. * vfsmount_lock must be held for write
  1899. */
  1900. static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts)
  1901. {
  1902. LIST_HEAD(graveyard);
  1903. struct vfsmount *m;
  1904. /* extract submounts of 'mountpoint' from the expiration list */
  1905. while (select_submounts(mnt, &graveyard)) {
  1906. while (!list_empty(&graveyard)) {
  1907. m = list_first_entry(&graveyard, struct vfsmount,
  1908. mnt_expire);
  1909. touch_mnt_namespace(m->mnt_ns);
  1910. umount_tree(m, 1, umounts);
  1911. }
  1912. }
  1913. }
  1914. /*
  1915. * Some copy_from_user() implementations do not return the exact number of
  1916. * bytes remaining to copy on a fault. But copy_mount_options() requires that.
  1917. * Note that this function differs from copy_from_user() in that it will oops
  1918. * on bad values of `to', rather than returning a short copy.
  1919. */
  1920. static long exact_copy_from_user(void *to, const void __user * from,
  1921. unsigned long n)
  1922. {
  1923. char *t = to;
  1924. const char __user *f = from;
  1925. char c;
  1926. if (!access_ok(VERIFY_READ, from, n))
  1927. return n;
  1928. while (n) {
  1929. if (__get_user(c, f)) {
  1930. memset(t, 0, n);
  1931. break;
  1932. }
  1933. *t++ = c;
  1934. f++;
  1935. n--;
  1936. }
  1937. return n;
  1938. }
  1939. int copy_mount_options(const void __user * data, unsigned long *where)
  1940. {
  1941. int i;
  1942. unsigned long page;
  1943. unsigned long size;
  1944. *where = 0;
  1945. if (!data)
  1946. return 0;
  1947. if (!(page = __get_free_page(GFP_KERNEL)))
  1948. return -ENOMEM;
  1949. /* We only care that *some* data at the address the user
  1950. * gave us is valid. Just in case, we'll zero
  1951. * the remainder of the page.
  1952. */
  1953. /* copy_from_user cannot cross TASK_SIZE ! */
  1954. size = TASK_SIZE - (unsigned long)data;
  1955. if (size > PAGE_SIZE)
  1956. size = PAGE_SIZE;
  1957. i = size - exact_copy_from_user((void *)page, data, size);
  1958. if (!i) {
  1959. free_page(page);
  1960. return -EFAULT;
  1961. }
  1962. if (i != PAGE_SIZE)
  1963. memset((char *)page + i, 0, PAGE_SIZE - i);
  1964. *where = page;
  1965. return 0;
  1966. }
  1967. int copy_mount_string(const void __user *data, char **where)
  1968. {
  1969. char *tmp;
  1970. if (!data) {
  1971. *where = NULL;
  1972. return 0;
  1973. }
  1974. tmp = strndup_user(data, PAGE_SIZE);
  1975. if (IS_ERR(tmp))
  1976. return PTR_ERR(tmp);
  1977. *where = tmp;
  1978. return 0;
  1979. }
  1980. /*
  1981. * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
  1982. * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
  1983. *
  1984. * data is a (void *) that can point to any structure up to
  1985. * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
  1986. * information (or be NULL).
  1987. *
  1988. * Pre-0.97 versions of mount() didn't have a flags word.
  1989. * When the flags word was introduced its top half was required
  1990. * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
  1991. * Therefore, if this magic number is present, it carries no information
  1992. * and must be discarded.
  1993. */
  1994. long do_mount(char *dev_name, char *dir_name, char *type_page,
  1995. unsigned long flags, void *data_page)
  1996. {
  1997. struct path path;
  1998. int retval = 0;
  1999. int mnt_flags = 0;
  2000. /* Discard magic */
  2001. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  2002. flags &= ~MS_MGC_MSK;
  2003. /* Basic sanity checks */
  2004. if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
  2005. return -EINVAL;
  2006. if (data_page)
  2007. ((char *)data_page)[PAGE_SIZE - 1] = 0;
  2008. /* ... and get the mountpoint */
  2009. retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
  2010. if (retval)
  2011. return retval;
  2012. retval = security_sb_mount(dev_name, &path,
  2013. type_page, flags, data_page);
  2014. if (retval)
  2015. goto dput_out;
  2016. /* Default to relatime unless overriden */
  2017. if (!(flags & MS_NOATIME))
  2018. mnt_flags |= MNT_RELATIME;
  2019. /* Separate the per-mountpoint flags */
  2020. if (flags & MS_NOSUID)
  2021. mnt_flags |= MNT_NOSUID;
  2022. if (flags & MS_NODEV)
  2023. mnt_flags |= MNT_NODEV;
  2024. if (flags & MS_NOEXEC)
  2025. mnt_flags |= MNT_NOEXEC;
  2026. if (flags & MS_NOATIME)
  2027. mnt_flags |= MNT_NOATIME;
  2028. if (flags & MS_NODIRATIME)
  2029. mnt_flags |= MNT_NODIRATIME;
  2030. if (flags & MS_STRICTATIME)
  2031. mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
  2032. if (flags & MS_RDONLY)
  2033. mnt_flags |= MNT_READONLY;
  2034. flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
  2035. MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
  2036. MS_STRICTATIME);
  2037. if (flags & MS_REMOUNT)
  2038. retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
  2039. data_page);
  2040. else if (flags & MS_BIND)
  2041. retval = do_loopback(&path, dev_name, flags & MS_REC);
  2042. else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  2043. retval = do_change_type(&path, flags);
  2044. else if (flags & MS_MOVE)
  2045. retval = do_move_mount(&path, dev_name);
  2046. else
  2047. retval = do_new_mount(&path, type_page, flags, mnt_flags,
  2048. dev_name, data_page);
  2049. dput_out:
  2050. path_put(&path);
  2051. return retval;
  2052. }
  2053. static struct mnt_namespace *alloc_mnt_ns(void)
  2054. {
  2055. struct mnt_namespace *new_ns;
  2056. new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
  2057. if (!new_ns)
  2058. return ERR_PTR(-ENOMEM);
  2059. atomic_set(&new_ns->count, 1);
  2060. new_ns->root = NULL;
  2061. INIT_LIST_HEAD(&new_ns->list);
  2062. init_waitqueue_head(&new_ns->poll);
  2063. new_ns->event = 0;
  2064. return new_ns;
  2065. }
  2066. void mnt_make_longterm(struct vfsmount *mnt)
  2067. {
  2068. __mnt_make_longterm(mnt);
  2069. }
  2070. void mnt_make_shortterm(struct vfsmount *mnt)
  2071. {
  2072. #ifdef CONFIG_SMP
  2073. if (atomic_add_unless(&mnt->mnt_longterm, -1, 1))
  2074. return;
  2075. br_write_lock(vfsmount_lock);
  2076. atomic_dec(&mnt->mnt_longterm);
  2077. br_write_unlock(vfsmount_lock);
  2078. #endif
  2079. }
  2080. /*
  2081. * Allocate a new namespace structure and populate it with contents
  2082. * copied from the namespace of the passed in task structure.
  2083. */
  2084. static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
  2085. struct fs_struct *fs)
  2086. {
  2087. struct mnt_namespace *new_ns;
  2088. struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
  2089. struct vfsmount *p, *q;
  2090. new_ns = alloc_mnt_ns();
  2091. if (IS_ERR(new_ns))
  2092. return new_ns;
  2093. down_write(&namespace_sem);
  2094. /* First pass: copy the tree topology */
  2095. new_ns->root = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root,
  2096. CL_COPY_ALL | CL_EXPIRE);
  2097. if (!new_ns->root) {
  2098. up_write(&namespace_sem);
  2099. kfree(new_ns);
  2100. return ERR_PTR(-ENOMEM);
  2101. }
  2102. br_write_lock(vfsmount_lock);
  2103. list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
  2104. br_write_unlock(vfsmount_lock);
  2105. /*
  2106. * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
  2107. * as belonging to new namespace. We have already acquired a private
  2108. * fs_struct, so tsk->fs->lock is not needed.
  2109. */
  2110. p = mnt_ns->root;
  2111. q = new_ns->root;
  2112. while (p) {
  2113. q->mnt_ns = new_ns;
  2114. __mnt_make_longterm(q);
  2115. if (fs) {
  2116. if (p == fs->root.mnt) {
  2117. fs->root.mnt = mntget(q);
  2118. __mnt_make_longterm(q);
  2119. mnt_make_shortterm(p);
  2120. rootmnt = p;
  2121. }
  2122. if (p == fs->pwd.mnt) {
  2123. fs->pwd.mnt = mntget(q);
  2124. __mnt_make_longterm(q);
  2125. mnt_make_shortterm(p);
  2126. pwdmnt = p;
  2127. }
  2128. }
  2129. p = next_mnt(p, mnt_ns->root);
  2130. q = next_mnt(q, new_ns->root);
  2131. }
  2132. up_write(&namespace_sem);
  2133. if (rootmnt)
  2134. mntput(rootmnt);
  2135. if (pwdmnt)
  2136. mntput(pwdmnt);
  2137. return new_ns;
  2138. }
  2139. struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
  2140. struct fs_struct *new_fs)
  2141. {
  2142. struct mnt_namespace *new_ns;
  2143. BUG_ON(!ns);
  2144. get_mnt_ns(ns);
  2145. if (!(flags & CLONE_NEWNS))
  2146. return ns;
  2147. new_ns = dup_mnt_ns(ns, new_fs);
  2148. put_mnt_ns(ns);
  2149. return new_ns;
  2150. }
  2151. /**
  2152. * create_mnt_ns - creates a private namespace and adds a root filesystem
  2153. * @mnt: pointer to the new root filesystem mountpoint
  2154. */
  2155. static struct mnt_namespace *create_mnt_ns(struct vfsmount *mnt)
  2156. {
  2157. struct mnt_namespace *new_ns;
  2158. new_ns = alloc_mnt_ns();
  2159. if (!IS_ERR(new_ns)) {
  2160. mnt->mnt_ns = new_ns;
  2161. __mnt_make_longterm(mnt);
  2162. new_ns->root = mnt;
  2163. list_add(&new_ns->list, &new_ns->root->mnt_list);
  2164. } else {
  2165. mntput(mnt);
  2166. }
  2167. return new_ns;
  2168. }
  2169. struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
  2170. {
  2171. struct mnt_namespace *ns;
  2172. struct super_block *s;
  2173. struct path path;
  2174. int err;
  2175. ns = create_mnt_ns(mnt);
  2176. if (IS_ERR(ns))
  2177. return ERR_CAST(ns);
  2178. err = vfs_path_lookup(mnt->mnt_root, mnt,
  2179. name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
  2180. put_mnt_ns(ns);
  2181. if (err)
  2182. return ERR_PTR(err);
  2183. /* trade a vfsmount reference for active sb one */
  2184. s = path.mnt->mnt_sb;
  2185. atomic_inc(&s->s_active);
  2186. mntput(path.mnt);
  2187. /* lock the sucker */
  2188. down_write(&s->s_umount);
  2189. /* ... and return the root of (sub)tree on it */
  2190. return path.dentry;
  2191. }
  2192. EXPORT_SYMBOL(mount_subtree);
  2193. SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
  2194. char __user *, type, unsigned long, flags, void __user *, data)
  2195. {
  2196. int ret;
  2197. char *kernel_type;
  2198. char *kernel_dir;
  2199. char *kernel_dev;
  2200. unsigned long data_page;
  2201. ret = copy_mount_string(type, &kernel_type);
  2202. if (ret < 0)
  2203. goto out_type;
  2204. kernel_dir = getname(dir_name);
  2205. if (IS_ERR(kernel_dir)) {
  2206. ret = PTR_ERR(kernel_dir);
  2207. goto out_dir;
  2208. }
  2209. ret = copy_mount_string(dev_name, &kernel_dev);
  2210. if (ret < 0)
  2211. goto out_dev;
  2212. ret = copy_mount_options(data, &data_page);
  2213. if (ret < 0)
  2214. goto out_data;
  2215. ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags,
  2216. (void *) data_page);
  2217. free_page(data_page);
  2218. out_data:
  2219. kfree(kernel_dev);
  2220. out_dev:
  2221. putname(kernel_dir);
  2222. out_dir:
  2223. kfree(kernel_type);
  2224. out_type:
  2225. return ret;
  2226. }
  2227. /*
  2228. * Return true if path is reachable from root
  2229. *
  2230. * namespace_sem or vfsmount_lock is held
  2231. */
  2232. bool is_path_reachable(struct vfsmount *mnt, struct dentry *dentry,
  2233. const struct path *root)
  2234. {
  2235. while (mnt != root->mnt && mnt_has_parent(mnt)) {
  2236. dentry = mnt->mnt_mountpoint;
  2237. mnt = mnt->mnt_parent;
  2238. }
  2239. return mnt == root->mnt && is_subdir(dentry, root->dentry);
  2240. }
  2241. int path_is_under(struct path *path1, struct path *path2)
  2242. {
  2243. int res;
  2244. br_read_lock(vfsmount_lock);
  2245. res = is_path_reachable(path1->mnt, path1->dentry, path2);
  2246. br_read_unlock(vfsmount_lock);
  2247. return res;
  2248. }
  2249. EXPORT_SYMBOL(path_is_under);
  2250. /*
  2251. * pivot_root Semantics:
  2252. * Moves the root file system of the current process to the directory put_old,
  2253. * makes new_root as the new root file system of the current process, and sets
  2254. * root/cwd of all processes which had them on the current root to new_root.
  2255. *
  2256. * Restrictions:
  2257. * The new_root and put_old must be directories, and must not be on the
  2258. * same file system as the current process root. The put_old must be
  2259. * underneath new_root, i.e. adding a non-zero number of /.. to the string
  2260. * pointed to by put_old must yield the same directory as new_root. No other
  2261. * file system may be mounted on put_old. After all, new_root is a mountpoint.
  2262. *
  2263. * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
  2264. * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
  2265. * in this situation.
  2266. *
  2267. * Notes:
  2268. * - we don't move root/cwd if they are not at the root (reason: if something
  2269. * cared enough to change them, it's probably wrong to force them elsewhere)
  2270. * - it's okay to pick a root that isn't the root of a file system, e.g.
  2271. * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
  2272. * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
  2273. * first.
  2274. */
  2275. SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
  2276. const char __user *, put_old)
  2277. {
  2278. struct path new, old, parent_path, root_parent, root;
  2279. int error;
  2280. if (!capable(CAP_SYS_ADMIN))
  2281. return -EPERM;
  2282. error = user_path_dir(new_root, &new);
  2283. if (error)
  2284. goto out0;
  2285. error = user_path_dir(put_old, &old);
  2286. if (error)
  2287. goto out1;
  2288. error = security_sb_pivotroot(&old, &new);
  2289. if (error)
  2290. goto out2;
  2291. get_fs_root(current->fs, &root);
  2292. error = lock_mount(&old);
  2293. if (error)
  2294. goto out3;
  2295. error = -EINVAL;
  2296. if (IS_MNT_SHARED(old.mnt) ||
  2297. IS_MNT_SHARED(new.mnt->mnt_parent) ||
  2298. IS_MNT_SHARED(root.mnt->mnt_parent))
  2299. goto out4;
  2300. if (!check_mnt(root.mnt) || !check_mnt(new.mnt))
  2301. goto out4;
  2302. error = -ENOENT;
  2303. if (d_unlinked(new.dentry))
  2304. goto out4;
  2305. if (d_unlinked(old.dentry))
  2306. goto out4;
  2307. error = -EBUSY;
  2308. if (new.mnt == root.mnt ||
  2309. old.mnt == root.mnt)
  2310. goto out4; /* loop, on the same file system */
  2311. error = -EINVAL;
  2312. if (root.mnt->mnt_root != root.dentry)
  2313. goto out4; /* not a mountpoint */
  2314. if (!mnt_has_parent(root.mnt))
  2315. goto out4; /* not attached */
  2316. if (new.mnt->mnt_root != new.dentry)
  2317. goto out4; /* not a mountpoint */
  2318. if (!mnt_has_parent(new.mnt))
  2319. goto out4; /* not attached */
  2320. /* make sure we can reach put_old from new_root */
  2321. if (!is_path_reachable(old.mnt, old.dentry, &new))
  2322. goto out4;
  2323. br_write_lock(vfsmount_lock);
  2324. detach_mnt(new.mnt, &parent_path);
  2325. detach_mnt(root.mnt, &root_parent);
  2326. /* mount old root on put_old */
  2327. attach_mnt(root.mnt, &old);
  2328. /* mount new_root on / */
  2329. attach_mnt(new.mnt, &root_parent);
  2330. touch_mnt_namespace(current->nsproxy->mnt_ns);
  2331. br_write_unlock(vfsmount_lock);
  2332. chroot_fs_refs(&root, &new);
  2333. error = 0;
  2334. out4:
  2335. unlock_mount(&old);
  2336. if (!error) {
  2337. path_put(&root_parent);
  2338. path_put(&parent_path);
  2339. }
  2340. out3:
  2341. path_put(&root);
  2342. out2:
  2343. path_put(&old);
  2344. out1:
  2345. path_put(&new);
  2346. out0:
  2347. return error;
  2348. }
  2349. static void __init init_mount_tree(void)
  2350. {
  2351. struct vfsmount *mnt;
  2352. struct mnt_namespace *ns;
  2353. struct path root;
  2354. mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
  2355. if (IS_ERR(mnt))
  2356. panic("Can't create rootfs");
  2357. ns = create_mnt_ns(mnt);
  2358. if (IS_ERR(ns))
  2359. panic("Can't allocate initial namespace");
  2360. init_task.nsproxy->mnt_ns = ns;
  2361. get_mnt_ns(ns);
  2362. root.mnt = ns->root;
  2363. root.dentry = ns->root->mnt_root;
  2364. set_fs_pwd(current->fs, &root);
  2365. set_fs_root(current->fs, &root);
  2366. }
  2367. void __init mnt_init(void)
  2368. {
  2369. unsigned u;
  2370. int err;
  2371. init_rwsem(&namespace_sem);
  2372. mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
  2373. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  2374. mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
  2375. if (!mount_hashtable)
  2376. panic("Failed to allocate mount hash table\n");
  2377. printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE);
  2378. for (u = 0; u < HASH_SIZE; u++)
  2379. INIT_LIST_HEAD(&mount_hashtable[u]);
  2380. br_lock_init(vfsmount_lock);
  2381. err = sysfs_init();
  2382. if (err)
  2383. printk(KERN_WARNING "%s: sysfs_init error: %d\n",
  2384. __func__, err);
  2385. fs_kobj = kobject_create_and_add("fs", NULL);
  2386. if (!fs_kobj)
  2387. printk(KERN_WARNING "%s: kobj create error\n", __func__);
  2388. init_rootfs();
  2389. init_mount_tree();
  2390. }
  2391. void put_mnt_ns(struct mnt_namespace *ns)
  2392. {
  2393. LIST_HEAD(umount_list);
  2394. if (!atomic_dec_and_test(&ns->count))
  2395. return;
  2396. down_write(&namespace_sem);
  2397. br_write_lock(vfsmount_lock);
  2398. umount_tree(ns->root, 0, &umount_list);
  2399. br_write_unlock(vfsmount_lock);
  2400. up_write(&namespace_sem);
  2401. release_mounts(&umount_list);
  2402. kfree(ns);
  2403. }
  2404. struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
  2405. {
  2406. struct vfsmount *mnt;
  2407. mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
  2408. if (!IS_ERR(mnt)) {
  2409. /*
  2410. * it is a longterm mount, don't release mnt until
  2411. * we unmount before file sys is unregistered
  2412. */
  2413. mnt_make_longterm(mnt);
  2414. }
  2415. return mnt;
  2416. }
  2417. EXPORT_SYMBOL_GPL(kern_mount_data);
  2418. void kern_unmount(struct vfsmount *mnt)
  2419. {
  2420. /* release long term mount so mount point can be released */
  2421. if (!IS_ERR_OR_NULL(mnt)) {
  2422. mnt_make_shortterm(mnt);
  2423. mntput(mnt);
  2424. }
  2425. }
  2426. EXPORT_SYMBOL(kern_unmount);
  2427. bool our_mnt(struct vfsmount *mnt)
  2428. {
  2429. return check_mnt(mnt);
  2430. }