namespace.c 59 KB

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