shmem.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667
  1. /*
  2. * Resizable virtual memory filesystem for Linux.
  3. *
  4. * Copyright (C) 2000 Linus Torvalds.
  5. * 2000 Transmeta Corp.
  6. * 2000-2001 Christoph Rohland
  7. * 2000-2001 SAP AG
  8. * 2002 Red Hat Inc.
  9. * Copyright (C) 2002-2011 Hugh Dickins.
  10. * Copyright (C) 2011 Google Inc.
  11. * Copyright (C) 2002-2005 VERITAS Software Corporation.
  12. * Copyright (C) 2004 Andi Kleen, SuSE Labs
  13. *
  14. * Extended attribute support for tmpfs:
  15. * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
  16. * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  17. *
  18. * tiny-shmem:
  19. * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
  20. *
  21. * This file is released under the GPL.
  22. */
  23. #include <linux/fs.h>
  24. #include <linux/init.h>
  25. #include <linux/vfs.h>
  26. #include <linux/mount.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/file.h>
  29. #include <linux/mm.h>
  30. #include <linux/export.h>
  31. #include <linux/swap.h>
  32. static struct vfsmount *shm_mnt;
  33. #ifdef CONFIG_SHMEM
  34. /*
  35. * This virtual memory filesystem is heavily based on the ramfs. It
  36. * extends ramfs by the ability to use swap and honor resource limits
  37. * which makes it a completely usable filesystem.
  38. */
  39. #include <linux/xattr.h>
  40. #include <linux/exportfs.h>
  41. #include <linux/posix_acl.h>
  42. #include <linux/generic_acl.h>
  43. #include <linux/mman.h>
  44. #include <linux/string.h>
  45. #include <linux/slab.h>
  46. #include <linux/backing-dev.h>
  47. #include <linux/shmem_fs.h>
  48. #include <linux/writeback.h>
  49. #include <linux/blkdev.h>
  50. #include <linux/pagevec.h>
  51. #include <linux/percpu_counter.h>
  52. #include <linux/splice.h>
  53. #include <linux/security.h>
  54. #include <linux/swapops.h>
  55. #include <linux/mempolicy.h>
  56. #include <linux/namei.h>
  57. #include <linux/ctype.h>
  58. #include <linux/migrate.h>
  59. #include <linux/highmem.h>
  60. #include <linux/seq_file.h>
  61. #include <linux/magic.h>
  62. #include <asm/uaccess.h>
  63. #include <asm/pgtable.h>
  64. #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
  65. #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
  66. /* Pretend that each entry is of this size in directory's i_size */
  67. #define BOGO_DIRENT_SIZE 20
  68. /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
  69. #define SHORT_SYMLINK_LEN 128
  70. struct shmem_xattr {
  71. struct list_head list; /* anchored by shmem_inode_info->xattr_list */
  72. char *name; /* xattr name */
  73. size_t size;
  74. char value[0];
  75. };
  76. /* Flag allocation requirements to shmem_getpage */
  77. enum sgp_type {
  78. SGP_READ, /* don't exceed i_size, don't allocate page */
  79. SGP_CACHE, /* don't exceed i_size, may allocate page */
  80. SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
  81. SGP_WRITE, /* may exceed i_size, may allocate page */
  82. };
  83. #ifdef CONFIG_TMPFS
  84. static unsigned long shmem_default_max_blocks(void)
  85. {
  86. return totalram_pages / 2;
  87. }
  88. static unsigned long shmem_default_max_inodes(void)
  89. {
  90. return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
  91. }
  92. #endif
  93. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  94. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
  95. static inline int shmem_getpage(struct inode *inode, pgoff_t index,
  96. struct page **pagep, enum sgp_type sgp, int *fault_type)
  97. {
  98. return shmem_getpage_gfp(inode, index, pagep, sgp,
  99. mapping_gfp_mask(inode->i_mapping), fault_type);
  100. }
  101. static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
  102. {
  103. return sb->s_fs_info;
  104. }
  105. /*
  106. * shmem_file_setup pre-accounts the whole fixed size of a VM object,
  107. * for shared memory and for shared anonymous (/dev/zero) mappings
  108. * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
  109. * consistent with the pre-accounting of private mappings ...
  110. */
  111. static inline int shmem_acct_size(unsigned long flags, loff_t size)
  112. {
  113. return (flags & VM_NORESERVE) ?
  114. 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
  115. }
  116. static inline void shmem_unacct_size(unsigned long flags, loff_t size)
  117. {
  118. if (!(flags & VM_NORESERVE))
  119. vm_unacct_memory(VM_ACCT(size));
  120. }
  121. /*
  122. * ... whereas tmpfs objects are accounted incrementally as
  123. * pages are allocated, in order to allow huge sparse files.
  124. * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
  125. * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
  126. */
  127. static inline int shmem_acct_block(unsigned long flags)
  128. {
  129. return (flags & VM_NORESERVE) ?
  130. security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
  131. }
  132. static inline void shmem_unacct_blocks(unsigned long flags, long pages)
  133. {
  134. if (flags & VM_NORESERVE)
  135. vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
  136. }
  137. static const struct super_operations shmem_ops;
  138. static const struct address_space_operations shmem_aops;
  139. static const struct file_operations shmem_file_operations;
  140. static const struct inode_operations shmem_inode_operations;
  141. static const struct inode_operations shmem_dir_inode_operations;
  142. static const struct inode_operations shmem_special_inode_operations;
  143. static const struct vm_operations_struct shmem_vm_ops;
  144. static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
  145. .ra_pages = 0, /* No readahead */
  146. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
  147. };
  148. static LIST_HEAD(shmem_swaplist);
  149. static DEFINE_MUTEX(shmem_swaplist_mutex);
  150. static int shmem_reserve_inode(struct super_block *sb)
  151. {
  152. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  153. if (sbinfo->max_inodes) {
  154. spin_lock(&sbinfo->stat_lock);
  155. if (!sbinfo->free_inodes) {
  156. spin_unlock(&sbinfo->stat_lock);
  157. return -ENOSPC;
  158. }
  159. sbinfo->free_inodes--;
  160. spin_unlock(&sbinfo->stat_lock);
  161. }
  162. return 0;
  163. }
  164. static void shmem_free_inode(struct super_block *sb)
  165. {
  166. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  167. if (sbinfo->max_inodes) {
  168. spin_lock(&sbinfo->stat_lock);
  169. sbinfo->free_inodes++;
  170. spin_unlock(&sbinfo->stat_lock);
  171. }
  172. }
  173. /**
  174. * shmem_recalc_inode - recalculate the block usage of an inode
  175. * @inode: inode to recalc
  176. *
  177. * We have to calculate the free blocks since the mm can drop
  178. * undirtied hole pages behind our back.
  179. *
  180. * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
  181. * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
  182. *
  183. * It has to be called with the spinlock held.
  184. */
  185. static void shmem_recalc_inode(struct inode *inode)
  186. {
  187. struct shmem_inode_info *info = SHMEM_I(inode);
  188. long freed;
  189. freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
  190. if (freed > 0) {
  191. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  192. if (sbinfo->max_blocks)
  193. percpu_counter_add(&sbinfo->used_blocks, -freed);
  194. info->alloced -= freed;
  195. inode->i_blocks -= freed * BLOCKS_PER_PAGE;
  196. shmem_unacct_blocks(info->flags, freed);
  197. }
  198. }
  199. /*
  200. * Replace item expected in radix tree by a new item, while holding tree lock.
  201. */
  202. static int shmem_radix_tree_replace(struct address_space *mapping,
  203. pgoff_t index, void *expected, void *replacement)
  204. {
  205. void **pslot;
  206. void *item = NULL;
  207. VM_BUG_ON(!expected);
  208. pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
  209. if (pslot)
  210. item = radix_tree_deref_slot_protected(pslot,
  211. &mapping->tree_lock);
  212. if (item != expected)
  213. return -ENOENT;
  214. if (replacement)
  215. radix_tree_replace_slot(pslot, replacement);
  216. else
  217. radix_tree_delete(&mapping->page_tree, index);
  218. return 0;
  219. }
  220. /*
  221. * Like add_to_page_cache_locked, but error if expected item has gone.
  222. */
  223. static int shmem_add_to_page_cache(struct page *page,
  224. struct address_space *mapping,
  225. pgoff_t index, gfp_t gfp, void *expected)
  226. {
  227. int error = 0;
  228. VM_BUG_ON(!PageLocked(page));
  229. VM_BUG_ON(!PageSwapBacked(page));
  230. if (!expected)
  231. error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
  232. if (!error) {
  233. page_cache_get(page);
  234. page->mapping = mapping;
  235. page->index = index;
  236. spin_lock_irq(&mapping->tree_lock);
  237. if (!expected)
  238. error = radix_tree_insert(&mapping->page_tree,
  239. index, page);
  240. else
  241. error = shmem_radix_tree_replace(mapping, index,
  242. expected, page);
  243. if (!error) {
  244. mapping->nrpages++;
  245. __inc_zone_page_state(page, NR_FILE_PAGES);
  246. __inc_zone_page_state(page, NR_SHMEM);
  247. spin_unlock_irq(&mapping->tree_lock);
  248. } else {
  249. page->mapping = NULL;
  250. spin_unlock_irq(&mapping->tree_lock);
  251. page_cache_release(page);
  252. }
  253. if (!expected)
  254. radix_tree_preload_end();
  255. }
  256. if (error)
  257. mem_cgroup_uncharge_cache_page(page);
  258. return error;
  259. }
  260. /*
  261. * Like delete_from_page_cache, but substitutes swap for page.
  262. */
  263. static void shmem_delete_from_page_cache(struct page *page, void *radswap)
  264. {
  265. struct address_space *mapping = page->mapping;
  266. int error;
  267. spin_lock_irq(&mapping->tree_lock);
  268. error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
  269. page->mapping = NULL;
  270. mapping->nrpages--;
  271. __dec_zone_page_state(page, NR_FILE_PAGES);
  272. __dec_zone_page_state(page, NR_SHMEM);
  273. spin_unlock_irq(&mapping->tree_lock);
  274. page_cache_release(page);
  275. BUG_ON(error);
  276. }
  277. /*
  278. * Like find_get_pages, but collecting swap entries as well as pages.
  279. */
  280. static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
  281. pgoff_t start, unsigned int nr_pages,
  282. struct page **pages, pgoff_t *indices)
  283. {
  284. unsigned int i;
  285. unsigned int ret;
  286. unsigned int nr_found;
  287. rcu_read_lock();
  288. restart:
  289. nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
  290. (void ***)pages, indices, start, nr_pages);
  291. ret = 0;
  292. for (i = 0; i < nr_found; i++) {
  293. struct page *page;
  294. repeat:
  295. page = radix_tree_deref_slot((void **)pages[i]);
  296. if (unlikely(!page))
  297. continue;
  298. if (radix_tree_exception(page)) {
  299. if (radix_tree_deref_retry(page))
  300. goto restart;
  301. /*
  302. * Otherwise, we must be storing a swap entry
  303. * here as an exceptional entry: so return it
  304. * without attempting to raise page count.
  305. */
  306. goto export;
  307. }
  308. if (!page_cache_get_speculative(page))
  309. goto repeat;
  310. /* Has the page moved? */
  311. if (unlikely(page != *((void **)pages[i]))) {
  312. page_cache_release(page);
  313. goto repeat;
  314. }
  315. export:
  316. indices[ret] = indices[i];
  317. pages[ret] = page;
  318. ret++;
  319. }
  320. if (unlikely(!ret && nr_found))
  321. goto restart;
  322. rcu_read_unlock();
  323. return ret;
  324. }
  325. /*
  326. * Remove swap entry from radix tree, free the swap and its page cache.
  327. */
  328. static int shmem_free_swap(struct address_space *mapping,
  329. pgoff_t index, void *radswap)
  330. {
  331. int error;
  332. spin_lock_irq(&mapping->tree_lock);
  333. error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
  334. spin_unlock_irq(&mapping->tree_lock);
  335. if (!error)
  336. free_swap_and_cache(radix_to_swp_entry(radswap));
  337. return error;
  338. }
  339. /*
  340. * Pagevec may contain swap entries, so shuffle up pages before releasing.
  341. */
  342. static void shmem_deswap_pagevec(struct pagevec *pvec)
  343. {
  344. int i, j;
  345. for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
  346. struct page *page = pvec->pages[i];
  347. if (!radix_tree_exceptional_entry(page))
  348. pvec->pages[j++] = page;
  349. }
  350. pvec->nr = j;
  351. }
  352. /*
  353. * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
  354. */
  355. void shmem_unlock_mapping(struct address_space *mapping)
  356. {
  357. struct pagevec pvec;
  358. pgoff_t indices[PAGEVEC_SIZE];
  359. pgoff_t index = 0;
  360. pagevec_init(&pvec, 0);
  361. /*
  362. * Minor point, but we might as well stop if someone else SHM_LOCKs it.
  363. */
  364. while (!mapping_unevictable(mapping)) {
  365. /*
  366. * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
  367. * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
  368. */
  369. pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
  370. PAGEVEC_SIZE, pvec.pages, indices);
  371. if (!pvec.nr)
  372. break;
  373. index = indices[pvec.nr - 1] + 1;
  374. shmem_deswap_pagevec(&pvec);
  375. check_move_unevictable_pages(pvec.pages, pvec.nr);
  376. pagevec_release(&pvec);
  377. cond_resched();
  378. }
  379. }
  380. /*
  381. * Remove range of pages and swap entries from radix tree, and free them.
  382. */
  383. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  384. {
  385. struct address_space *mapping = inode->i_mapping;
  386. struct shmem_inode_info *info = SHMEM_I(inode);
  387. pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  388. unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
  389. pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
  390. struct pagevec pvec;
  391. pgoff_t indices[PAGEVEC_SIZE];
  392. long nr_swaps_freed = 0;
  393. pgoff_t index;
  394. int i;
  395. BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
  396. pagevec_init(&pvec, 0);
  397. index = start;
  398. while (index <= end) {
  399. pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
  400. min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
  401. pvec.pages, indices);
  402. if (!pvec.nr)
  403. break;
  404. mem_cgroup_uncharge_start();
  405. for (i = 0; i < pagevec_count(&pvec); i++) {
  406. struct page *page = pvec.pages[i];
  407. index = indices[i];
  408. if (index > end)
  409. break;
  410. if (radix_tree_exceptional_entry(page)) {
  411. nr_swaps_freed += !shmem_free_swap(mapping,
  412. index, page);
  413. continue;
  414. }
  415. if (!trylock_page(page))
  416. continue;
  417. if (page->mapping == mapping) {
  418. VM_BUG_ON(PageWriteback(page));
  419. truncate_inode_page(mapping, page);
  420. }
  421. unlock_page(page);
  422. }
  423. shmem_deswap_pagevec(&pvec);
  424. pagevec_release(&pvec);
  425. mem_cgroup_uncharge_end();
  426. cond_resched();
  427. index++;
  428. }
  429. if (partial) {
  430. struct page *page = NULL;
  431. shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
  432. if (page) {
  433. zero_user_segment(page, partial, PAGE_CACHE_SIZE);
  434. set_page_dirty(page);
  435. unlock_page(page);
  436. page_cache_release(page);
  437. }
  438. }
  439. index = start;
  440. for ( ; ; ) {
  441. cond_resched();
  442. pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
  443. min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
  444. pvec.pages, indices);
  445. if (!pvec.nr) {
  446. if (index == start)
  447. break;
  448. index = start;
  449. continue;
  450. }
  451. if (index == start && indices[0] > end) {
  452. shmem_deswap_pagevec(&pvec);
  453. pagevec_release(&pvec);
  454. break;
  455. }
  456. mem_cgroup_uncharge_start();
  457. for (i = 0; i < pagevec_count(&pvec); i++) {
  458. struct page *page = pvec.pages[i];
  459. index = indices[i];
  460. if (index > end)
  461. break;
  462. if (radix_tree_exceptional_entry(page)) {
  463. nr_swaps_freed += !shmem_free_swap(mapping,
  464. index, page);
  465. continue;
  466. }
  467. lock_page(page);
  468. if (page->mapping == mapping) {
  469. VM_BUG_ON(PageWriteback(page));
  470. truncate_inode_page(mapping, page);
  471. }
  472. unlock_page(page);
  473. }
  474. shmem_deswap_pagevec(&pvec);
  475. pagevec_release(&pvec);
  476. mem_cgroup_uncharge_end();
  477. index++;
  478. }
  479. spin_lock(&info->lock);
  480. info->swapped -= nr_swaps_freed;
  481. shmem_recalc_inode(inode);
  482. spin_unlock(&info->lock);
  483. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  484. }
  485. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  486. static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
  487. {
  488. struct inode *inode = dentry->d_inode;
  489. int error;
  490. error = inode_change_ok(inode, attr);
  491. if (error)
  492. return error;
  493. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  494. loff_t oldsize = inode->i_size;
  495. loff_t newsize = attr->ia_size;
  496. if (newsize != oldsize) {
  497. i_size_write(inode, newsize);
  498. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  499. }
  500. if (newsize < oldsize) {
  501. loff_t holebegin = round_up(newsize, PAGE_SIZE);
  502. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  503. shmem_truncate_range(inode, newsize, (loff_t)-1);
  504. /* unmap again to remove racily COWed private pages */
  505. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  506. }
  507. }
  508. setattr_copy(inode, attr);
  509. #ifdef CONFIG_TMPFS_POSIX_ACL
  510. if (attr->ia_valid & ATTR_MODE)
  511. error = generic_acl_chmod(inode);
  512. #endif
  513. return error;
  514. }
  515. static void shmem_evict_inode(struct inode *inode)
  516. {
  517. struct shmem_inode_info *info = SHMEM_I(inode);
  518. struct shmem_xattr *xattr, *nxattr;
  519. if (inode->i_mapping->a_ops == &shmem_aops) {
  520. shmem_unacct_size(info->flags, inode->i_size);
  521. inode->i_size = 0;
  522. shmem_truncate_range(inode, 0, (loff_t)-1);
  523. if (!list_empty(&info->swaplist)) {
  524. mutex_lock(&shmem_swaplist_mutex);
  525. list_del_init(&info->swaplist);
  526. mutex_unlock(&shmem_swaplist_mutex);
  527. }
  528. } else
  529. kfree(info->symlink);
  530. list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
  531. kfree(xattr->name);
  532. kfree(xattr);
  533. }
  534. BUG_ON(inode->i_blocks);
  535. shmem_free_inode(inode->i_sb);
  536. end_writeback(inode);
  537. }
  538. /*
  539. * If swap found in inode, free it and move page from swapcache to filecache.
  540. */
  541. static int shmem_unuse_inode(struct shmem_inode_info *info,
  542. swp_entry_t swap, struct page *page)
  543. {
  544. struct address_space *mapping = info->vfs_inode.i_mapping;
  545. void *radswap;
  546. pgoff_t index;
  547. int error;
  548. radswap = swp_to_radix_entry(swap);
  549. index = radix_tree_locate_item(&mapping->page_tree, radswap);
  550. if (index == -1)
  551. return 0;
  552. /*
  553. * Move _head_ to start search for next from here.
  554. * But be careful: shmem_evict_inode checks list_empty without taking
  555. * mutex, and there's an instant in list_move_tail when info->swaplist
  556. * would appear empty, if it were the only one on shmem_swaplist.
  557. */
  558. if (shmem_swaplist.next != &info->swaplist)
  559. list_move_tail(&shmem_swaplist, &info->swaplist);
  560. /*
  561. * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
  562. * but also to hold up shmem_evict_inode(): so inode cannot be freed
  563. * beneath us (pagelock doesn't help until the page is in pagecache).
  564. */
  565. error = shmem_add_to_page_cache(page, mapping, index,
  566. GFP_NOWAIT, radswap);
  567. /* which does mem_cgroup_uncharge_cache_page on error */
  568. if (error != -ENOMEM) {
  569. /*
  570. * Truncation and eviction use free_swap_and_cache(), which
  571. * only does trylock page: if we raced, best clean up here.
  572. */
  573. delete_from_swap_cache(page);
  574. set_page_dirty(page);
  575. if (!error) {
  576. spin_lock(&info->lock);
  577. info->swapped--;
  578. spin_unlock(&info->lock);
  579. swap_free(swap);
  580. }
  581. error = 1; /* not an error, but entry was found */
  582. }
  583. return error;
  584. }
  585. /*
  586. * Search through swapped inodes to find and replace swap by page.
  587. */
  588. int shmem_unuse(swp_entry_t swap, struct page *page)
  589. {
  590. struct list_head *this, *next;
  591. struct shmem_inode_info *info;
  592. int found = 0;
  593. int error;
  594. /*
  595. * Charge page using GFP_KERNEL while we can wait, before taking
  596. * the shmem_swaplist_mutex which might hold up shmem_writepage().
  597. * Charged back to the user (not to caller) when swap account is used.
  598. */
  599. error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
  600. if (error)
  601. goto out;
  602. /* No radix_tree_preload: swap entry keeps a place for page in tree */
  603. mutex_lock(&shmem_swaplist_mutex);
  604. list_for_each_safe(this, next, &shmem_swaplist) {
  605. info = list_entry(this, struct shmem_inode_info, swaplist);
  606. if (info->swapped)
  607. found = shmem_unuse_inode(info, swap, page);
  608. else
  609. list_del_init(&info->swaplist);
  610. cond_resched();
  611. if (found)
  612. break;
  613. }
  614. mutex_unlock(&shmem_swaplist_mutex);
  615. if (!found)
  616. mem_cgroup_uncharge_cache_page(page);
  617. if (found < 0)
  618. error = found;
  619. out:
  620. unlock_page(page);
  621. page_cache_release(page);
  622. return error;
  623. }
  624. /*
  625. * Move the page from the page cache to the swap cache.
  626. */
  627. static int shmem_writepage(struct page *page, struct writeback_control *wbc)
  628. {
  629. struct shmem_inode_info *info;
  630. struct address_space *mapping;
  631. struct inode *inode;
  632. swp_entry_t swap;
  633. pgoff_t index;
  634. BUG_ON(!PageLocked(page));
  635. mapping = page->mapping;
  636. index = page->index;
  637. inode = mapping->host;
  638. info = SHMEM_I(inode);
  639. if (info->flags & VM_LOCKED)
  640. goto redirty;
  641. if (!total_swap_pages)
  642. goto redirty;
  643. /*
  644. * shmem_backing_dev_info's capabilities prevent regular writeback or
  645. * sync from ever calling shmem_writepage; but a stacking filesystem
  646. * might use ->writepage of its underlying filesystem, in which case
  647. * tmpfs should write out to swap only in response to memory pressure,
  648. * and not for the writeback threads or sync.
  649. */
  650. if (!wbc->for_reclaim) {
  651. WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
  652. goto redirty;
  653. }
  654. swap = get_swap_page();
  655. if (!swap.val)
  656. goto redirty;
  657. /*
  658. * Add inode to shmem_unuse()'s list of swapped-out inodes,
  659. * if it's not already there. Do it now before the page is
  660. * moved to swap cache, when its pagelock no longer protects
  661. * the inode from eviction. But don't unlock the mutex until
  662. * we've incremented swapped, because shmem_unuse_inode() will
  663. * prune a !swapped inode from the swaplist under this mutex.
  664. */
  665. mutex_lock(&shmem_swaplist_mutex);
  666. if (list_empty(&info->swaplist))
  667. list_add_tail(&info->swaplist, &shmem_swaplist);
  668. if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
  669. swap_shmem_alloc(swap);
  670. shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
  671. spin_lock(&info->lock);
  672. info->swapped++;
  673. shmem_recalc_inode(inode);
  674. spin_unlock(&info->lock);
  675. mutex_unlock(&shmem_swaplist_mutex);
  676. BUG_ON(page_mapped(page));
  677. swap_writepage(page, wbc);
  678. return 0;
  679. }
  680. mutex_unlock(&shmem_swaplist_mutex);
  681. swapcache_free(swap, NULL);
  682. redirty:
  683. set_page_dirty(page);
  684. if (wbc->for_reclaim)
  685. return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
  686. unlock_page(page);
  687. return 0;
  688. }
  689. #ifdef CONFIG_NUMA
  690. #ifdef CONFIG_TMPFS
  691. static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  692. {
  693. char buffer[64];
  694. if (!mpol || mpol->mode == MPOL_DEFAULT)
  695. return; /* show nothing */
  696. mpol_to_str(buffer, sizeof(buffer), mpol, 1);
  697. seq_printf(seq, ",mpol=%s", buffer);
  698. }
  699. static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  700. {
  701. struct mempolicy *mpol = NULL;
  702. if (sbinfo->mpol) {
  703. spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
  704. mpol = sbinfo->mpol;
  705. mpol_get(mpol);
  706. spin_unlock(&sbinfo->stat_lock);
  707. }
  708. return mpol;
  709. }
  710. #endif /* CONFIG_TMPFS */
  711. static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  712. struct shmem_inode_info *info, pgoff_t index)
  713. {
  714. struct mempolicy mpol, *spol;
  715. struct vm_area_struct pvma;
  716. spol = mpol_cond_copy(&mpol,
  717. mpol_shared_policy_lookup(&info->policy, index));
  718. /* Create a pseudo vma that just contains the policy */
  719. pvma.vm_start = 0;
  720. pvma.vm_pgoff = index;
  721. pvma.vm_ops = NULL;
  722. pvma.vm_policy = spol;
  723. return swapin_readahead(swap, gfp, &pvma, 0);
  724. }
  725. static struct page *shmem_alloc_page(gfp_t gfp,
  726. struct shmem_inode_info *info, pgoff_t index)
  727. {
  728. struct vm_area_struct pvma;
  729. /* Create a pseudo vma that just contains the policy */
  730. pvma.vm_start = 0;
  731. pvma.vm_pgoff = index;
  732. pvma.vm_ops = NULL;
  733. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
  734. /*
  735. * alloc_page_vma() will drop the shared policy reference
  736. */
  737. return alloc_page_vma(gfp, &pvma, 0);
  738. }
  739. #else /* !CONFIG_NUMA */
  740. #ifdef CONFIG_TMPFS
  741. static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  742. {
  743. }
  744. #endif /* CONFIG_TMPFS */
  745. static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  746. struct shmem_inode_info *info, pgoff_t index)
  747. {
  748. return swapin_readahead(swap, gfp, NULL, 0);
  749. }
  750. static inline struct page *shmem_alloc_page(gfp_t gfp,
  751. struct shmem_inode_info *info, pgoff_t index)
  752. {
  753. return alloc_page(gfp);
  754. }
  755. #endif /* CONFIG_NUMA */
  756. #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
  757. static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  758. {
  759. return NULL;
  760. }
  761. #endif
  762. /*
  763. * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
  764. *
  765. * If we allocate a new one we do not mark it dirty. That's up to the
  766. * vm. If we swap it in we mark it dirty since we also free the swap
  767. * entry since a page cannot live in both the swap and page cache
  768. */
  769. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  770. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
  771. {
  772. struct address_space *mapping = inode->i_mapping;
  773. struct shmem_inode_info *info;
  774. struct shmem_sb_info *sbinfo;
  775. struct page *page;
  776. swp_entry_t swap;
  777. int error;
  778. int once = 0;
  779. if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
  780. return -EFBIG;
  781. repeat:
  782. swap.val = 0;
  783. page = find_lock_page(mapping, index);
  784. if (radix_tree_exceptional_entry(page)) {
  785. swap = radix_to_swp_entry(page);
  786. page = NULL;
  787. }
  788. if (sgp != SGP_WRITE &&
  789. ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  790. error = -EINVAL;
  791. goto failed;
  792. }
  793. if (page || (sgp == SGP_READ && !swap.val)) {
  794. /*
  795. * Once we can get the page lock, it must be uptodate:
  796. * if there were an error in reading back from swap,
  797. * the page would not be inserted into the filecache.
  798. */
  799. BUG_ON(page && !PageUptodate(page));
  800. *pagep = page;
  801. return 0;
  802. }
  803. /*
  804. * Fast cache lookup did not find it:
  805. * bring it back from swap or allocate.
  806. */
  807. info = SHMEM_I(inode);
  808. sbinfo = SHMEM_SB(inode->i_sb);
  809. if (swap.val) {
  810. /* Look it up and read it in.. */
  811. page = lookup_swap_cache(swap);
  812. if (!page) {
  813. /* here we actually do the io */
  814. if (fault_type)
  815. *fault_type |= VM_FAULT_MAJOR;
  816. page = shmem_swapin(swap, gfp, info, index);
  817. if (!page) {
  818. error = -ENOMEM;
  819. goto failed;
  820. }
  821. }
  822. /* We have to do this with page locked to prevent races */
  823. lock_page(page);
  824. if (!PageUptodate(page)) {
  825. error = -EIO;
  826. goto failed;
  827. }
  828. wait_on_page_writeback(page);
  829. /* Someone may have already done it for us */
  830. if (page->mapping) {
  831. if (page->mapping == mapping &&
  832. page->index == index)
  833. goto done;
  834. error = -EEXIST;
  835. goto failed;
  836. }
  837. error = mem_cgroup_cache_charge(page, current->mm,
  838. gfp & GFP_RECLAIM_MASK);
  839. if (!error)
  840. error = shmem_add_to_page_cache(page, mapping, index,
  841. gfp, swp_to_radix_entry(swap));
  842. if (error)
  843. goto failed;
  844. spin_lock(&info->lock);
  845. info->swapped--;
  846. shmem_recalc_inode(inode);
  847. spin_unlock(&info->lock);
  848. delete_from_swap_cache(page);
  849. set_page_dirty(page);
  850. swap_free(swap);
  851. } else {
  852. if (shmem_acct_block(info->flags)) {
  853. error = -ENOSPC;
  854. goto failed;
  855. }
  856. if (sbinfo->max_blocks) {
  857. if (percpu_counter_compare(&sbinfo->used_blocks,
  858. sbinfo->max_blocks) >= 0) {
  859. error = -ENOSPC;
  860. goto unacct;
  861. }
  862. percpu_counter_inc(&sbinfo->used_blocks);
  863. }
  864. page = shmem_alloc_page(gfp, info, index);
  865. if (!page) {
  866. error = -ENOMEM;
  867. goto decused;
  868. }
  869. SetPageSwapBacked(page);
  870. __set_page_locked(page);
  871. error = mem_cgroup_cache_charge(page, current->mm,
  872. gfp & GFP_RECLAIM_MASK);
  873. if (!error)
  874. error = shmem_add_to_page_cache(page, mapping, index,
  875. gfp, NULL);
  876. if (error)
  877. goto decused;
  878. lru_cache_add_anon(page);
  879. spin_lock(&info->lock);
  880. info->alloced++;
  881. inode->i_blocks += BLOCKS_PER_PAGE;
  882. shmem_recalc_inode(inode);
  883. spin_unlock(&info->lock);
  884. clear_highpage(page);
  885. flush_dcache_page(page);
  886. SetPageUptodate(page);
  887. if (sgp == SGP_DIRTY)
  888. set_page_dirty(page);
  889. }
  890. done:
  891. /* Perhaps the file has been truncated since we checked */
  892. if (sgp != SGP_WRITE &&
  893. ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  894. error = -EINVAL;
  895. goto trunc;
  896. }
  897. *pagep = page;
  898. return 0;
  899. /*
  900. * Error recovery.
  901. */
  902. trunc:
  903. ClearPageDirty(page);
  904. delete_from_page_cache(page);
  905. spin_lock(&info->lock);
  906. info->alloced--;
  907. inode->i_blocks -= BLOCKS_PER_PAGE;
  908. spin_unlock(&info->lock);
  909. decused:
  910. if (sbinfo->max_blocks)
  911. percpu_counter_add(&sbinfo->used_blocks, -1);
  912. unacct:
  913. shmem_unacct_blocks(info->flags, 1);
  914. failed:
  915. if (swap.val && error != -EINVAL) {
  916. struct page *test = find_get_page(mapping, index);
  917. if (test && !radix_tree_exceptional_entry(test))
  918. page_cache_release(test);
  919. /* Have another try if the entry has changed */
  920. if (test != swp_to_radix_entry(swap))
  921. error = -EEXIST;
  922. }
  923. if (page) {
  924. unlock_page(page);
  925. page_cache_release(page);
  926. }
  927. if (error == -ENOSPC && !once++) {
  928. info = SHMEM_I(inode);
  929. spin_lock(&info->lock);
  930. shmem_recalc_inode(inode);
  931. spin_unlock(&info->lock);
  932. goto repeat;
  933. }
  934. if (error == -EEXIST)
  935. goto repeat;
  936. return error;
  937. }
  938. static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  939. {
  940. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  941. int error;
  942. int ret = VM_FAULT_LOCKED;
  943. error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
  944. if (error)
  945. return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
  946. if (ret & VM_FAULT_MAJOR) {
  947. count_vm_event(PGMAJFAULT);
  948. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  949. }
  950. return ret;
  951. }
  952. #ifdef CONFIG_NUMA
  953. static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
  954. {
  955. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  956. return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
  957. }
  958. static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
  959. unsigned long addr)
  960. {
  961. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  962. pgoff_t index;
  963. index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  964. return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
  965. }
  966. #endif
  967. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  968. {
  969. struct inode *inode = file->f_path.dentry->d_inode;
  970. struct shmem_inode_info *info = SHMEM_I(inode);
  971. int retval = -ENOMEM;
  972. spin_lock(&info->lock);
  973. if (lock && !(info->flags & VM_LOCKED)) {
  974. if (!user_shm_lock(inode->i_size, user))
  975. goto out_nomem;
  976. info->flags |= VM_LOCKED;
  977. mapping_set_unevictable(file->f_mapping);
  978. }
  979. if (!lock && (info->flags & VM_LOCKED) && user) {
  980. user_shm_unlock(inode->i_size, user);
  981. info->flags &= ~VM_LOCKED;
  982. mapping_clear_unevictable(file->f_mapping);
  983. }
  984. retval = 0;
  985. out_nomem:
  986. spin_unlock(&info->lock);
  987. return retval;
  988. }
  989. static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
  990. {
  991. file_accessed(file);
  992. vma->vm_ops = &shmem_vm_ops;
  993. vma->vm_flags |= VM_CAN_NONLINEAR;
  994. return 0;
  995. }
  996. static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
  997. umode_t mode, dev_t dev, unsigned long flags)
  998. {
  999. struct inode *inode;
  1000. struct shmem_inode_info *info;
  1001. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1002. if (shmem_reserve_inode(sb))
  1003. return NULL;
  1004. inode = new_inode(sb);
  1005. if (inode) {
  1006. inode->i_ino = get_next_ino();
  1007. inode_init_owner(inode, dir, mode);
  1008. inode->i_blocks = 0;
  1009. inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
  1010. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1011. inode->i_generation = get_seconds();
  1012. info = SHMEM_I(inode);
  1013. memset(info, 0, (char *)inode - (char *)info);
  1014. spin_lock_init(&info->lock);
  1015. info->flags = flags & VM_NORESERVE;
  1016. INIT_LIST_HEAD(&info->swaplist);
  1017. INIT_LIST_HEAD(&info->xattr_list);
  1018. cache_no_acl(inode);
  1019. switch (mode & S_IFMT) {
  1020. default:
  1021. inode->i_op = &shmem_special_inode_operations;
  1022. init_special_inode(inode, mode, dev);
  1023. break;
  1024. case S_IFREG:
  1025. inode->i_mapping->a_ops = &shmem_aops;
  1026. inode->i_op = &shmem_inode_operations;
  1027. inode->i_fop = &shmem_file_operations;
  1028. mpol_shared_policy_init(&info->policy,
  1029. shmem_get_sbmpol(sbinfo));
  1030. break;
  1031. case S_IFDIR:
  1032. inc_nlink(inode);
  1033. /* Some things misbehave if size == 0 on a directory */
  1034. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  1035. inode->i_op = &shmem_dir_inode_operations;
  1036. inode->i_fop = &simple_dir_operations;
  1037. break;
  1038. case S_IFLNK:
  1039. /*
  1040. * Must not load anything in the rbtree,
  1041. * mpol_free_shared_policy will not be called.
  1042. */
  1043. mpol_shared_policy_init(&info->policy, NULL);
  1044. break;
  1045. }
  1046. } else
  1047. shmem_free_inode(sb);
  1048. return inode;
  1049. }
  1050. #ifdef CONFIG_TMPFS
  1051. static const struct inode_operations shmem_symlink_inode_operations;
  1052. static const struct inode_operations shmem_short_symlink_operations;
  1053. #ifdef CONFIG_TMPFS_XATTR
  1054. static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
  1055. #else
  1056. #define shmem_initxattrs NULL
  1057. #endif
  1058. static int
  1059. shmem_write_begin(struct file *file, struct address_space *mapping,
  1060. loff_t pos, unsigned len, unsigned flags,
  1061. struct page **pagep, void **fsdata)
  1062. {
  1063. struct inode *inode = mapping->host;
  1064. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1065. return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
  1066. }
  1067. static int
  1068. shmem_write_end(struct file *file, struct address_space *mapping,
  1069. loff_t pos, unsigned len, unsigned copied,
  1070. struct page *page, void *fsdata)
  1071. {
  1072. struct inode *inode = mapping->host;
  1073. if (pos + copied > inode->i_size)
  1074. i_size_write(inode, pos + copied);
  1075. set_page_dirty(page);
  1076. unlock_page(page);
  1077. page_cache_release(page);
  1078. return copied;
  1079. }
  1080. static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
  1081. {
  1082. struct inode *inode = filp->f_path.dentry->d_inode;
  1083. struct address_space *mapping = inode->i_mapping;
  1084. pgoff_t index;
  1085. unsigned long offset;
  1086. enum sgp_type sgp = SGP_READ;
  1087. /*
  1088. * Might this read be for a stacking filesystem? Then when reading
  1089. * holes of a sparse file, we actually need to allocate those pages,
  1090. * and even mark them dirty, so it cannot exceed the max_blocks limit.
  1091. */
  1092. if (segment_eq(get_fs(), KERNEL_DS))
  1093. sgp = SGP_DIRTY;
  1094. index = *ppos >> PAGE_CACHE_SHIFT;
  1095. offset = *ppos & ~PAGE_CACHE_MASK;
  1096. for (;;) {
  1097. struct page *page = NULL;
  1098. pgoff_t end_index;
  1099. unsigned long nr, ret;
  1100. loff_t i_size = i_size_read(inode);
  1101. end_index = i_size >> PAGE_CACHE_SHIFT;
  1102. if (index > end_index)
  1103. break;
  1104. if (index == end_index) {
  1105. nr = i_size & ~PAGE_CACHE_MASK;
  1106. if (nr <= offset)
  1107. break;
  1108. }
  1109. desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
  1110. if (desc->error) {
  1111. if (desc->error == -EINVAL)
  1112. desc->error = 0;
  1113. break;
  1114. }
  1115. if (page)
  1116. unlock_page(page);
  1117. /*
  1118. * We must evaluate after, since reads (unlike writes)
  1119. * are called without i_mutex protection against truncate
  1120. */
  1121. nr = PAGE_CACHE_SIZE;
  1122. i_size = i_size_read(inode);
  1123. end_index = i_size >> PAGE_CACHE_SHIFT;
  1124. if (index == end_index) {
  1125. nr = i_size & ~PAGE_CACHE_MASK;
  1126. if (nr <= offset) {
  1127. if (page)
  1128. page_cache_release(page);
  1129. break;
  1130. }
  1131. }
  1132. nr -= offset;
  1133. if (page) {
  1134. /*
  1135. * If users can be writing to this page using arbitrary
  1136. * virtual addresses, take care about potential aliasing
  1137. * before reading the page on the kernel side.
  1138. */
  1139. if (mapping_writably_mapped(mapping))
  1140. flush_dcache_page(page);
  1141. /*
  1142. * Mark the page accessed if we read the beginning.
  1143. */
  1144. if (!offset)
  1145. mark_page_accessed(page);
  1146. } else {
  1147. page = ZERO_PAGE(0);
  1148. page_cache_get(page);
  1149. }
  1150. /*
  1151. * Ok, we have the page, and it's up-to-date, so
  1152. * now we can copy it to user space...
  1153. *
  1154. * The actor routine returns how many bytes were actually used..
  1155. * NOTE! This may not be the same as how much of a user buffer
  1156. * we filled up (we may be padding etc), so we can only update
  1157. * "pos" here (the actor routine has to update the user buffer
  1158. * pointers and the remaining count).
  1159. */
  1160. ret = actor(desc, page, offset, nr);
  1161. offset += ret;
  1162. index += offset >> PAGE_CACHE_SHIFT;
  1163. offset &= ~PAGE_CACHE_MASK;
  1164. page_cache_release(page);
  1165. if (ret != nr || !desc->count)
  1166. break;
  1167. cond_resched();
  1168. }
  1169. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  1170. file_accessed(filp);
  1171. }
  1172. static ssize_t shmem_file_aio_read(struct kiocb *iocb,
  1173. const struct iovec *iov, unsigned long nr_segs, loff_t pos)
  1174. {
  1175. struct file *filp = iocb->ki_filp;
  1176. ssize_t retval;
  1177. unsigned long seg;
  1178. size_t count;
  1179. loff_t *ppos = &iocb->ki_pos;
  1180. retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
  1181. if (retval)
  1182. return retval;
  1183. for (seg = 0; seg < nr_segs; seg++) {
  1184. read_descriptor_t desc;
  1185. desc.written = 0;
  1186. desc.arg.buf = iov[seg].iov_base;
  1187. desc.count = iov[seg].iov_len;
  1188. if (desc.count == 0)
  1189. continue;
  1190. desc.error = 0;
  1191. do_shmem_file_read(filp, ppos, &desc, file_read_actor);
  1192. retval += desc.written;
  1193. if (desc.error) {
  1194. retval = retval ?: desc.error;
  1195. break;
  1196. }
  1197. if (desc.count > 0)
  1198. break;
  1199. }
  1200. return retval;
  1201. }
  1202. static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
  1203. struct pipe_inode_info *pipe, size_t len,
  1204. unsigned int flags)
  1205. {
  1206. struct address_space *mapping = in->f_mapping;
  1207. struct inode *inode = mapping->host;
  1208. unsigned int loff, nr_pages, req_pages;
  1209. struct page *pages[PIPE_DEF_BUFFERS];
  1210. struct partial_page partial[PIPE_DEF_BUFFERS];
  1211. struct page *page;
  1212. pgoff_t index, end_index;
  1213. loff_t isize, left;
  1214. int error, page_nr;
  1215. struct splice_pipe_desc spd = {
  1216. .pages = pages,
  1217. .partial = partial,
  1218. .flags = flags,
  1219. .ops = &page_cache_pipe_buf_ops,
  1220. .spd_release = spd_release_page,
  1221. };
  1222. isize = i_size_read(inode);
  1223. if (unlikely(*ppos >= isize))
  1224. return 0;
  1225. left = isize - *ppos;
  1226. if (unlikely(left < len))
  1227. len = left;
  1228. if (splice_grow_spd(pipe, &spd))
  1229. return -ENOMEM;
  1230. index = *ppos >> PAGE_CACHE_SHIFT;
  1231. loff = *ppos & ~PAGE_CACHE_MASK;
  1232. req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1233. nr_pages = min(req_pages, pipe->buffers);
  1234. spd.nr_pages = find_get_pages_contig(mapping, index,
  1235. nr_pages, spd.pages);
  1236. index += spd.nr_pages;
  1237. error = 0;
  1238. while (spd.nr_pages < nr_pages) {
  1239. error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
  1240. if (error)
  1241. break;
  1242. unlock_page(page);
  1243. spd.pages[spd.nr_pages++] = page;
  1244. index++;
  1245. }
  1246. index = *ppos >> PAGE_CACHE_SHIFT;
  1247. nr_pages = spd.nr_pages;
  1248. spd.nr_pages = 0;
  1249. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  1250. unsigned int this_len;
  1251. if (!len)
  1252. break;
  1253. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  1254. page = spd.pages[page_nr];
  1255. if (!PageUptodate(page) || page->mapping != mapping) {
  1256. error = shmem_getpage(inode, index, &page,
  1257. SGP_CACHE, NULL);
  1258. if (error)
  1259. break;
  1260. unlock_page(page);
  1261. page_cache_release(spd.pages[page_nr]);
  1262. spd.pages[page_nr] = page;
  1263. }
  1264. isize = i_size_read(inode);
  1265. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  1266. if (unlikely(!isize || index > end_index))
  1267. break;
  1268. if (end_index == index) {
  1269. unsigned int plen;
  1270. plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  1271. if (plen <= loff)
  1272. break;
  1273. this_len = min(this_len, plen - loff);
  1274. len = this_len;
  1275. }
  1276. spd.partial[page_nr].offset = loff;
  1277. spd.partial[page_nr].len = this_len;
  1278. len -= this_len;
  1279. loff = 0;
  1280. spd.nr_pages++;
  1281. index++;
  1282. }
  1283. while (page_nr < nr_pages)
  1284. page_cache_release(spd.pages[page_nr++]);
  1285. if (spd.nr_pages)
  1286. error = splice_to_pipe(pipe, &spd);
  1287. splice_shrink_spd(pipe, &spd);
  1288. if (error > 0) {
  1289. *ppos += error;
  1290. file_accessed(in);
  1291. }
  1292. return error;
  1293. }
  1294. static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
  1295. {
  1296. struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
  1297. buf->f_type = TMPFS_MAGIC;
  1298. buf->f_bsize = PAGE_CACHE_SIZE;
  1299. buf->f_namelen = NAME_MAX;
  1300. if (sbinfo->max_blocks) {
  1301. buf->f_blocks = sbinfo->max_blocks;
  1302. buf->f_bavail =
  1303. buf->f_bfree = sbinfo->max_blocks -
  1304. percpu_counter_sum(&sbinfo->used_blocks);
  1305. }
  1306. if (sbinfo->max_inodes) {
  1307. buf->f_files = sbinfo->max_inodes;
  1308. buf->f_ffree = sbinfo->free_inodes;
  1309. }
  1310. /* else leave those fields 0 like simple_statfs */
  1311. return 0;
  1312. }
  1313. /*
  1314. * File creation. Allocate an inode, and we're done..
  1315. */
  1316. static int
  1317. shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  1318. {
  1319. struct inode *inode;
  1320. int error = -ENOSPC;
  1321. inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
  1322. if (inode) {
  1323. error = security_inode_init_security(inode, dir,
  1324. &dentry->d_name,
  1325. shmem_initxattrs, NULL);
  1326. if (error) {
  1327. if (error != -EOPNOTSUPP) {
  1328. iput(inode);
  1329. return error;
  1330. }
  1331. }
  1332. #ifdef CONFIG_TMPFS_POSIX_ACL
  1333. error = generic_acl_init(inode, dir);
  1334. if (error) {
  1335. iput(inode);
  1336. return error;
  1337. }
  1338. #else
  1339. error = 0;
  1340. #endif
  1341. dir->i_size += BOGO_DIRENT_SIZE;
  1342. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1343. d_instantiate(dentry, inode);
  1344. dget(dentry); /* Extra count - pin the dentry in core */
  1345. }
  1346. return error;
  1347. }
  1348. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  1349. {
  1350. int error;
  1351. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  1352. return error;
  1353. inc_nlink(dir);
  1354. return 0;
  1355. }
  1356. static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  1357. struct nameidata *nd)
  1358. {
  1359. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  1360. }
  1361. /*
  1362. * Link a file..
  1363. */
  1364. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1365. {
  1366. struct inode *inode = old_dentry->d_inode;
  1367. int ret;
  1368. /*
  1369. * No ordinary (disk based) filesystem counts links as inodes;
  1370. * but each new link needs a new dentry, pinning lowmem, and
  1371. * tmpfs dentries cannot be pruned until they are unlinked.
  1372. */
  1373. ret = shmem_reserve_inode(inode->i_sb);
  1374. if (ret)
  1375. goto out;
  1376. dir->i_size += BOGO_DIRENT_SIZE;
  1377. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1378. inc_nlink(inode);
  1379. ihold(inode); /* New dentry reference */
  1380. dget(dentry); /* Extra pinning count for the created dentry */
  1381. d_instantiate(dentry, inode);
  1382. out:
  1383. return ret;
  1384. }
  1385. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  1386. {
  1387. struct inode *inode = dentry->d_inode;
  1388. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
  1389. shmem_free_inode(inode->i_sb);
  1390. dir->i_size -= BOGO_DIRENT_SIZE;
  1391. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1392. drop_nlink(inode);
  1393. dput(dentry); /* Undo the count from "create" - this does all the work */
  1394. return 0;
  1395. }
  1396. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  1397. {
  1398. if (!simple_empty(dentry))
  1399. return -ENOTEMPTY;
  1400. drop_nlink(dentry->d_inode);
  1401. drop_nlink(dir);
  1402. return shmem_unlink(dir, dentry);
  1403. }
  1404. /*
  1405. * The VFS layer already does all the dentry stuff for rename,
  1406. * we just have to decrement the usage count for the target if
  1407. * it exists so that the VFS layer correctly free's it when it
  1408. * gets overwritten.
  1409. */
  1410. static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  1411. {
  1412. struct inode *inode = old_dentry->d_inode;
  1413. int they_are_dirs = S_ISDIR(inode->i_mode);
  1414. if (!simple_empty(new_dentry))
  1415. return -ENOTEMPTY;
  1416. if (new_dentry->d_inode) {
  1417. (void) shmem_unlink(new_dir, new_dentry);
  1418. if (they_are_dirs)
  1419. drop_nlink(old_dir);
  1420. } else if (they_are_dirs) {
  1421. drop_nlink(old_dir);
  1422. inc_nlink(new_dir);
  1423. }
  1424. old_dir->i_size -= BOGO_DIRENT_SIZE;
  1425. new_dir->i_size += BOGO_DIRENT_SIZE;
  1426. old_dir->i_ctime = old_dir->i_mtime =
  1427. new_dir->i_ctime = new_dir->i_mtime =
  1428. inode->i_ctime = CURRENT_TIME;
  1429. return 0;
  1430. }
  1431. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1432. {
  1433. int error;
  1434. int len;
  1435. struct inode *inode;
  1436. struct page *page;
  1437. char *kaddr;
  1438. struct shmem_inode_info *info;
  1439. len = strlen(symname) + 1;
  1440. if (len > PAGE_CACHE_SIZE)
  1441. return -ENAMETOOLONG;
  1442. inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
  1443. if (!inode)
  1444. return -ENOSPC;
  1445. error = security_inode_init_security(inode, dir, &dentry->d_name,
  1446. shmem_initxattrs, NULL);
  1447. if (error) {
  1448. if (error != -EOPNOTSUPP) {
  1449. iput(inode);
  1450. return error;
  1451. }
  1452. error = 0;
  1453. }
  1454. info = SHMEM_I(inode);
  1455. inode->i_size = len-1;
  1456. if (len <= SHORT_SYMLINK_LEN) {
  1457. info->symlink = kmemdup(symname, len, GFP_KERNEL);
  1458. if (!info->symlink) {
  1459. iput(inode);
  1460. return -ENOMEM;
  1461. }
  1462. inode->i_op = &shmem_short_symlink_operations;
  1463. } else {
  1464. error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
  1465. if (error) {
  1466. iput(inode);
  1467. return error;
  1468. }
  1469. inode->i_mapping->a_ops = &shmem_aops;
  1470. inode->i_op = &shmem_symlink_inode_operations;
  1471. kaddr = kmap_atomic(page);
  1472. memcpy(kaddr, symname, len);
  1473. kunmap_atomic(kaddr);
  1474. set_page_dirty(page);
  1475. unlock_page(page);
  1476. page_cache_release(page);
  1477. }
  1478. dir->i_size += BOGO_DIRENT_SIZE;
  1479. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1480. d_instantiate(dentry, inode);
  1481. dget(dentry);
  1482. return 0;
  1483. }
  1484. static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
  1485. {
  1486. nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
  1487. return NULL;
  1488. }
  1489. static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
  1490. {
  1491. struct page *page = NULL;
  1492. int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
  1493. nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
  1494. if (page)
  1495. unlock_page(page);
  1496. return page;
  1497. }
  1498. static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  1499. {
  1500. if (!IS_ERR(nd_get_link(nd))) {
  1501. struct page *page = cookie;
  1502. kunmap(page);
  1503. mark_page_accessed(page);
  1504. page_cache_release(page);
  1505. }
  1506. }
  1507. #ifdef CONFIG_TMPFS_XATTR
  1508. /*
  1509. * Superblocks without xattr inode operations may get some security.* xattr
  1510. * support from the LSM "for free". As soon as we have any other xattrs
  1511. * like ACLs, we also need to implement the security.* handlers at
  1512. * filesystem level, though.
  1513. */
  1514. /*
  1515. * Allocate new xattr and copy in the value; but leave the name to callers.
  1516. */
  1517. static struct shmem_xattr *shmem_xattr_alloc(const void *value, size_t size)
  1518. {
  1519. struct shmem_xattr *new_xattr;
  1520. size_t len;
  1521. /* wrap around? */
  1522. len = sizeof(*new_xattr) + size;
  1523. if (len <= sizeof(*new_xattr))
  1524. return NULL;
  1525. new_xattr = kmalloc(len, GFP_KERNEL);
  1526. if (!new_xattr)
  1527. return NULL;
  1528. new_xattr->size = size;
  1529. memcpy(new_xattr->value, value, size);
  1530. return new_xattr;
  1531. }
  1532. /*
  1533. * Callback for security_inode_init_security() for acquiring xattrs.
  1534. */
  1535. static int shmem_initxattrs(struct inode *inode,
  1536. const struct xattr *xattr_array,
  1537. void *fs_info)
  1538. {
  1539. struct shmem_inode_info *info = SHMEM_I(inode);
  1540. const struct xattr *xattr;
  1541. struct shmem_xattr *new_xattr;
  1542. size_t len;
  1543. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  1544. new_xattr = shmem_xattr_alloc(xattr->value, xattr->value_len);
  1545. if (!new_xattr)
  1546. return -ENOMEM;
  1547. len = strlen(xattr->name) + 1;
  1548. new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
  1549. GFP_KERNEL);
  1550. if (!new_xattr->name) {
  1551. kfree(new_xattr);
  1552. return -ENOMEM;
  1553. }
  1554. memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
  1555. XATTR_SECURITY_PREFIX_LEN);
  1556. memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
  1557. xattr->name, len);
  1558. spin_lock(&info->lock);
  1559. list_add(&new_xattr->list, &info->xattr_list);
  1560. spin_unlock(&info->lock);
  1561. }
  1562. return 0;
  1563. }
  1564. static int shmem_xattr_get(struct dentry *dentry, const char *name,
  1565. void *buffer, size_t size)
  1566. {
  1567. struct shmem_inode_info *info;
  1568. struct shmem_xattr *xattr;
  1569. int ret = -ENODATA;
  1570. info = SHMEM_I(dentry->d_inode);
  1571. spin_lock(&info->lock);
  1572. list_for_each_entry(xattr, &info->xattr_list, list) {
  1573. if (strcmp(name, xattr->name))
  1574. continue;
  1575. ret = xattr->size;
  1576. if (buffer) {
  1577. if (size < xattr->size)
  1578. ret = -ERANGE;
  1579. else
  1580. memcpy(buffer, xattr->value, xattr->size);
  1581. }
  1582. break;
  1583. }
  1584. spin_unlock(&info->lock);
  1585. return ret;
  1586. }
  1587. static int shmem_xattr_set(struct inode *inode, const char *name,
  1588. const void *value, size_t size, int flags)
  1589. {
  1590. struct shmem_inode_info *info = SHMEM_I(inode);
  1591. struct shmem_xattr *xattr;
  1592. struct shmem_xattr *new_xattr = NULL;
  1593. int err = 0;
  1594. /* value == NULL means remove */
  1595. if (value) {
  1596. new_xattr = shmem_xattr_alloc(value, size);
  1597. if (!new_xattr)
  1598. return -ENOMEM;
  1599. new_xattr->name = kstrdup(name, GFP_KERNEL);
  1600. if (!new_xattr->name) {
  1601. kfree(new_xattr);
  1602. return -ENOMEM;
  1603. }
  1604. }
  1605. spin_lock(&info->lock);
  1606. list_for_each_entry(xattr, &info->xattr_list, list) {
  1607. if (!strcmp(name, xattr->name)) {
  1608. if (flags & XATTR_CREATE) {
  1609. xattr = new_xattr;
  1610. err = -EEXIST;
  1611. } else if (new_xattr) {
  1612. list_replace(&xattr->list, &new_xattr->list);
  1613. } else {
  1614. list_del(&xattr->list);
  1615. }
  1616. goto out;
  1617. }
  1618. }
  1619. if (flags & XATTR_REPLACE) {
  1620. xattr = new_xattr;
  1621. err = -ENODATA;
  1622. } else {
  1623. list_add(&new_xattr->list, &info->xattr_list);
  1624. xattr = NULL;
  1625. }
  1626. out:
  1627. spin_unlock(&info->lock);
  1628. if (xattr)
  1629. kfree(xattr->name);
  1630. kfree(xattr);
  1631. return err;
  1632. }
  1633. static const struct xattr_handler *shmem_xattr_handlers[] = {
  1634. #ifdef CONFIG_TMPFS_POSIX_ACL
  1635. &generic_acl_access_handler,
  1636. &generic_acl_default_handler,
  1637. #endif
  1638. NULL
  1639. };
  1640. static int shmem_xattr_validate(const char *name)
  1641. {
  1642. struct { const char *prefix; size_t len; } arr[] = {
  1643. { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
  1644. { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
  1645. };
  1646. int i;
  1647. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1648. size_t preflen = arr[i].len;
  1649. if (strncmp(name, arr[i].prefix, preflen) == 0) {
  1650. if (!name[preflen])
  1651. return -EINVAL;
  1652. return 0;
  1653. }
  1654. }
  1655. return -EOPNOTSUPP;
  1656. }
  1657. static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
  1658. void *buffer, size_t size)
  1659. {
  1660. int err;
  1661. /*
  1662. * If this is a request for a synthetic attribute in the system.*
  1663. * namespace use the generic infrastructure to resolve a handler
  1664. * for it via sb->s_xattr.
  1665. */
  1666. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1667. return generic_getxattr(dentry, name, buffer, size);
  1668. err = shmem_xattr_validate(name);
  1669. if (err)
  1670. return err;
  1671. return shmem_xattr_get(dentry, name, buffer, size);
  1672. }
  1673. static int shmem_setxattr(struct dentry *dentry, const char *name,
  1674. const void *value, size_t size, int flags)
  1675. {
  1676. int err;
  1677. /*
  1678. * If this is a request for a synthetic attribute in the system.*
  1679. * namespace use the generic infrastructure to resolve a handler
  1680. * for it via sb->s_xattr.
  1681. */
  1682. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1683. return generic_setxattr(dentry, name, value, size, flags);
  1684. err = shmem_xattr_validate(name);
  1685. if (err)
  1686. return err;
  1687. if (size == 0)
  1688. value = ""; /* empty EA, do not remove */
  1689. return shmem_xattr_set(dentry->d_inode, name, value, size, flags);
  1690. }
  1691. static int shmem_removexattr(struct dentry *dentry, const char *name)
  1692. {
  1693. int err;
  1694. /*
  1695. * If this is a request for a synthetic attribute in the system.*
  1696. * namespace use the generic infrastructure to resolve a handler
  1697. * for it via sb->s_xattr.
  1698. */
  1699. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1700. return generic_removexattr(dentry, name);
  1701. err = shmem_xattr_validate(name);
  1702. if (err)
  1703. return err;
  1704. return shmem_xattr_set(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
  1705. }
  1706. static bool xattr_is_trusted(const char *name)
  1707. {
  1708. return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
  1709. }
  1710. static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
  1711. {
  1712. bool trusted = capable(CAP_SYS_ADMIN);
  1713. struct shmem_xattr *xattr;
  1714. struct shmem_inode_info *info;
  1715. size_t used = 0;
  1716. info = SHMEM_I(dentry->d_inode);
  1717. spin_lock(&info->lock);
  1718. list_for_each_entry(xattr, &info->xattr_list, list) {
  1719. size_t len;
  1720. /* skip "trusted." attributes for unprivileged callers */
  1721. if (!trusted && xattr_is_trusted(xattr->name))
  1722. continue;
  1723. len = strlen(xattr->name) + 1;
  1724. used += len;
  1725. if (buffer) {
  1726. if (size < used) {
  1727. used = -ERANGE;
  1728. break;
  1729. }
  1730. memcpy(buffer, xattr->name, len);
  1731. buffer += len;
  1732. }
  1733. }
  1734. spin_unlock(&info->lock);
  1735. return used;
  1736. }
  1737. #endif /* CONFIG_TMPFS_XATTR */
  1738. static const struct inode_operations shmem_short_symlink_operations = {
  1739. .readlink = generic_readlink,
  1740. .follow_link = shmem_follow_short_symlink,
  1741. #ifdef CONFIG_TMPFS_XATTR
  1742. .setxattr = shmem_setxattr,
  1743. .getxattr = shmem_getxattr,
  1744. .listxattr = shmem_listxattr,
  1745. .removexattr = shmem_removexattr,
  1746. #endif
  1747. };
  1748. static const struct inode_operations shmem_symlink_inode_operations = {
  1749. .readlink = generic_readlink,
  1750. .follow_link = shmem_follow_link,
  1751. .put_link = shmem_put_link,
  1752. #ifdef CONFIG_TMPFS_XATTR
  1753. .setxattr = shmem_setxattr,
  1754. .getxattr = shmem_getxattr,
  1755. .listxattr = shmem_listxattr,
  1756. .removexattr = shmem_removexattr,
  1757. #endif
  1758. };
  1759. static struct dentry *shmem_get_parent(struct dentry *child)
  1760. {
  1761. return ERR_PTR(-ESTALE);
  1762. }
  1763. static int shmem_match(struct inode *ino, void *vfh)
  1764. {
  1765. __u32 *fh = vfh;
  1766. __u64 inum = fh[2];
  1767. inum = (inum << 32) | fh[1];
  1768. return ino->i_ino == inum && fh[0] == ino->i_generation;
  1769. }
  1770. static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
  1771. struct fid *fid, int fh_len, int fh_type)
  1772. {
  1773. struct inode *inode;
  1774. struct dentry *dentry = NULL;
  1775. u64 inum = fid->raw[2];
  1776. inum = (inum << 32) | fid->raw[1];
  1777. if (fh_len < 3)
  1778. return NULL;
  1779. inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
  1780. shmem_match, fid->raw);
  1781. if (inode) {
  1782. dentry = d_find_alias(inode);
  1783. iput(inode);
  1784. }
  1785. return dentry;
  1786. }
  1787. static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
  1788. int connectable)
  1789. {
  1790. struct inode *inode = dentry->d_inode;
  1791. if (*len < 3) {
  1792. *len = 3;
  1793. return 255;
  1794. }
  1795. if (inode_unhashed(inode)) {
  1796. /* Unfortunately insert_inode_hash is not idempotent,
  1797. * so as we hash inodes here rather than at creation
  1798. * time, we need a lock to ensure we only try
  1799. * to do it once
  1800. */
  1801. static DEFINE_SPINLOCK(lock);
  1802. spin_lock(&lock);
  1803. if (inode_unhashed(inode))
  1804. __insert_inode_hash(inode,
  1805. inode->i_ino + inode->i_generation);
  1806. spin_unlock(&lock);
  1807. }
  1808. fh[0] = inode->i_generation;
  1809. fh[1] = inode->i_ino;
  1810. fh[2] = ((__u64)inode->i_ino) >> 32;
  1811. *len = 3;
  1812. return 1;
  1813. }
  1814. static const struct export_operations shmem_export_ops = {
  1815. .get_parent = shmem_get_parent,
  1816. .encode_fh = shmem_encode_fh,
  1817. .fh_to_dentry = shmem_fh_to_dentry,
  1818. };
  1819. static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
  1820. bool remount)
  1821. {
  1822. char *this_char, *value, *rest;
  1823. while (options != NULL) {
  1824. this_char = options;
  1825. for (;;) {
  1826. /*
  1827. * NUL-terminate this option: unfortunately,
  1828. * mount options form a comma-separated list,
  1829. * but mpol's nodelist may also contain commas.
  1830. */
  1831. options = strchr(options, ',');
  1832. if (options == NULL)
  1833. break;
  1834. options++;
  1835. if (!isdigit(*options)) {
  1836. options[-1] = '\0';
  1837. break;
  1838. }
  1839. }
  1840. if (!*this_char)
  1841. continue;
  1842. if ((value = strchr(this_char,'=')) != NULL) {
  1843. *value++ = 0;
  1844. } else {
  1845. printk(KERN_ERR
  1846. "tmpfs: No value for mount option '%s'\n",
  1847. this_char);
  1848. return 1;
  1849. }
  1850. if (!strcmp(this_char,"size")) {
  1851. unsigned long long size;
  1852. size = memparse(value,&rest);
  1853. if (*rest == '%') {
  1854. size <<= PAGE_SHIFT;
  1855. size *= totalram_pages;
  1856. do_div(size, 100);
  1857. rest++;
  1858. }
  1859. if (*rest)
  1860. goto bad_val;
  1861. sbinfo->max_blocks =
  1862. DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
  1863. } else if (!strcmp(this_char,"nr_blocks")) {
  1864. sbinfo->max_blocks = memparse(value, &rest);
  1865. if (*rest)
  1866. goto bad_val;
  1867. } else if (!strcmp(this_char,"nr_inodes")) {
  1868. sbinfo->max_inodes = memparse(value, &rest);
  1869. if (*rest)
  1870. goto bad_val;
  1871. } else if (!strcmp(this_char,"mode")) {
  1872. if (remount)
  1873. continue;
  1874. sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
  1875. if (*rest)
  1876. goto bad_val;
  1877. } else if (!strcmp(this_char,"uid")) {
  1878. if (remount)
  1879. continue;
  1880. sbinfo->uid = simple_strtoul(value, &rest, 0);
  1881. if (*rest)
  1882. goto bad_val;
  1883. } else if (!strcmp(this_char,"gid")) {
  1884. if (remount)
  1885. continue;
  1886. sbinfo->gid = simple_strtoul(value, &rest, 0);
  1887. if (*rest)
  1888. goto bad_val;
  1889. } else if (!strcmp(this_char,"mpol")) {
  1890. if (mpol_parse_str(value, &sbinfo->mpol, 1))
  1891. goto bad_val;
  1892. } else {
  1893. printk(KERN_ERR "tmpfs: Bad mount option %s\n",
  1894. this_char);
  1895. return 1;
  1896. }
  1897. }
  1898. return 0;
  1899. bad_val:
  1900. printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
  1901. value, this_char);
  1902. return 1;
  1903. }
  1904. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  1905. {
  1906. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1907. struct shmem_sb_info config = *sbinfo;
  1908. unsigned long inodes;
  1909. int error = -EINVAL;
  1910. if (shmem_parse_options(data, &config, true))
  1911. return error;
  1912. spin_lock(&sbinfo->stat_lock);
  1913. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  1914. if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
  1915. goto out;
  1916. if (config.max_inodes < inodes)
  1917. goto out;
  1918. /*
  1919. * Those tests disallow limited->unlimited while any are in use;
  1920. * but we must separately disallow unlimited->limited, because
  1921. * in that case we have no record of how much is already in use.
  1922. */
  1923. if (config.max_blocks && !sbinfo->max_blocks)
  1924. goto out;
  1925. if (config.max_inodes && !sbinfo->max_inodes)
  1926. goto out;
  1927. error = 0;
  1928. sbinfo->max_blocks = config.max_blocks;
  1929. sbinfo->max_inodes = config.max_inodes;
  1930. sbinfo->free_inodes = config.max_inodes - inodes;
  1931. mpol_put(sbinfo->mpol);
  1932. sbinfo->mpol = config.mpol; /* transfers initial ref */
  1933. out:
  1934. spin_unlock(&sbinfo->stat_lock);
  1935. return error;
  1936. }
  1937. static int shmem_show_options(struct seq_file *seq, struct dentry *root)
  1938. {
  1939. struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
  1940. if (sbinfo->max_blocks != shmem_default_max_blocks())
  1941. seq_printf(seq, ",size=%luk",
  1942. sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
  1943. if (sbinfo->max_inodes != shmem_default_max_inodes())
  1944. seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
  1945. if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
  1946. seq_printf(seq, ",mode=%03ho", sbinfo->mode);
  1947. if (sbinfo->uid != 0)
  1948. seq_printf(seq, ",uid=%u", sbinfo->uid);
  1949. if (sbinfo->gid != 0)
  1950. seq_printf(seq, ",gid=%u", sbinfo->gid);
  1951. shmem_show_mpol(seq, sbinfo->mpol);
  1952. return 0;
  1953. }
  1954. #endif /* CONFIG_TMPFS */
  1955. static void shmem_put_super(struct super_block *sb)
  1956. {
  1957. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1958. percpu_counter_destroy(&sbinfo->used_blocks);
  1959. kfree(sbinfo);
  1960. sb->s_fs_info = NULL;
  1961. }
  1962. int shmem_fill_super(struct super_block *sb, void *data, int silent)
  1963. {
  1964. struct inode *inode;
  1965. struct shmem_sb_info *sbinfo;
  1966. int err = -ENOMEM;
  1967. /* Round up to L1_CACHE_BYTES to resist false sharing */
  1968. sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
  1969. L1_CACHE_BYTES), GFP_KERNEL);
  1970. if (!sbinfo)
  1971. return -ENOMEM;
  1972. sbinfo->mode = S_IRWXUGO | S_ISVTX;
  1973. sbinfo->uid = current_fsuid();
  1974. sbinfo->gid = current_fsgid();
  1975. sb->s_fs_info = sbinfo;
  1976. #ifdef CONFIG_TMPFS
  1977. /*
  1978. * Per default we only allow half of the physical ram per
  1979. * tmpfs instance, limiting inodes to one per page of lowmem;
  1980. * but the internal instance is left unlimited.
  1981. */
  1982. if (!(sb->s_flags & MS_NOUSER)) {
  1983. sbinfo->max_blocks = shmem_default_max_blocks();
  1984. sbinfo->max_inodes = shmem_default_max_inodes();
  1985. if (shmem_parse_options(data, sbinfo, false)) {
  1986. err = -EINVAL;
  1987. goto failed;
  1988. }
  1989. }
  1990. sb->s_export_op = &shmem_export_ops;
  1991. #else
  1992. sb->s_flags |= MS_NOUSER;
  1993. #endif
  1994. spin_lock_init(&sbinfo->stat_lock);
  1995. if (percpu_counter_init(&sbinfo->used_blocks, 0))
  1996. goto failed;
  1997. sbinfo->free_inodes = sbinfo->max_inodes;
  1998. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1999. sb->s_blocksize = PAGE_CACHE_SIZE;
  2000. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  2001. sb->s_magic = TMPFS_MAGIC;
  2002. sb->s_op = &shmem_ops;
  2003. sb->s_time_gran = 1;
  2004. #ifdef CONFIG_TMPFS_XATTR
  2005. sb->s_xattr = shmem_xattr_handlers;
  2006. #endif
  2007. #ifdef CONFIG_TMPFS_POSIX_ACL
  2008. sb->s_flags |= MS_POSIXACL;
  2009. #endif
  2010. inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
  2011. if (!inode)
  2012. goto failed;
  2013. inode->i_uid = sbinfo->uid;
  2014. inode->i_gid = sbinfo->gid;
  2015. sb->s_root = d_make_root(inode);
  2016. if (!sb->s_root)
  2017. goto failed;
  2018. return 0;
  2019. failed:
  2020. shmem_put_super(sb);
  2021. return err;
  2022. }
  2023. static struct kmem_cache *shmem_inode_cachep;
  2024. static struct inode *shmem_alloc_inode(struct super_block *sb)
  2025. {
  2026. struct shmem_inode_info *info;
  2027. info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
  2028. if (!info)
  2029. return NULL;
  2030. return &info->vfs_inode;
  2031. }
  2032. static void shmem_destroy_callback(struct rcu_head *head)
  2033. {
  2034. struct inode *inode = container_of(head, struct inode, i_rcu);
  2035. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  2036. }
  2037. static void shmem_destroy_inode(struct inode *inode)
  2038. {
  2039. if (S_ISREG(inode->i_mode))
  2040. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  2041. call_rcu(&inode->i_rcu, shmem_destroy_callback);
  2042. }
  2043. static void shmem_init_inode(void *foo)
  2044. {
  2045. struct shmem_inode_info *info = foo;
  2046. inode_init_once(&info->vfs_inode);
  2047. }
  2048. static int shmem_init_inodecache(void)
  2049. {
  2050. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  2051. sizeof(struct shmem_inode_info),
  2052. 0, SLAB_PANIC, shmem_init_inode);
  2053. return 0;
  2054. }
  2055. static void shmem_destroy_inodecache(void)
  2056. {
  2057. kmem_cache_destroy(shmem_inode_cachep);
  2058. }
  2059. static const struct address_space_operations shmem_aops = {
  2060. .writepage = shmem_writepage,
  2061. .set_page_dirty = __set_page_dirty_no_writeback,
  2062. #ifdef CONFIG_TMPFS
  2063. .write_begin = shmem_write_begin,
  2064. .write_end = shmem_write_end,
  2065. #endif
  2066. .migratepage = migrate_page,
  2067. .error_remove_page = generic_error_remove_page,
  2068. };
  2069. static const struct file_operations shmem_file_operations = {
  2070. .mmap = shmem_mmap,
  2071. #ifdef CONFIG_TMPFS
  2072. .llseek = generic_file_llseek,
  2073. .read = do_sync_read,
  2074. .write = do_sync_write,
  2075. .aio_read = shmem_file_aio_read,
  2076. .aio_write = generic_file_aio_write,
  2077. .fsync = noop_fsync,
  2078. .splice_read = shmem_file_splice_read,
  2079. .splice_write = generic_file_splice_write,
  2080. #endif
  2081. };
  2082. static const struct inode_operations shmem_inode_operations = {
  2083. .setattr = shmem_setattr,
  2084. .truncate_range = shmem_truncate_range,
  2085. #ifdef CONFIG_TMPFS_XATTR
  2086. .setxattr = shmem_setxattr,
  2087. .getxattr = shmem_getxattr,
  2088. .listxattr = shmem_listxattr,
  2089. .removexattr = shmem_removexattr,
  2090. #endif
  2091. };
  2092. static const struct inode_operations shmem_dir_inode_operations = {
  2093. #ifdef CONFIG_TMPFS
  2094. .create = shmem_create,
  2095. .lookup = simple_lookup,
  2096. .link = shmem_link,
  2097. .unlink = shmem_unlink,
  2098. .symlink = shmem_symlink,
  2099. .mkdir = shmem_mkdir,
  2100. .rmdir = shmem_rmdir,
  2101. .mknod = shmem_mknod,
  2102. .rename = shmem_rename,
  2103. #endif
  2104. #ifdef CONFIG_TMPFS_XATTR
  2105. .setxattr = shmem_setxattr,
  2106. .getxattr = shmem_getxattr,
  2107. .listxattr = shmem_listxattr,
  2108. .removexattr = shmem_removexattr,
  2109. #endif
  2110. #ifdef CONFIG_TMPFS_POSIX_ACL
  2111. .setattr = shmem_setattr,
  2112. #endif
  2113. };
  2114. static const struct inode_operations shmem_special_inode_operations = {
  2115. #ifdef CONFIG_TMPFS_XATTR
  2116. .setxattr = shmem_setxattr,
  2117. .getxattr = shmem_getxattr,
  2118. .listxattr = shmem_listxattr,
  2119. .removexattr = shmem_removexattr,
  2120. #endif
  2121. #ifdef CONFIG_TMPFS_POSIX_ACL
  2122. .setattr = shmem_setattr,
  2123. #endif
  2124. };
  2125. static const struct super_operations shmem_ops = {
  2126. .alloc_inode = shmem_alloc_inode,
  2127. .destroy_inode = shmem_destroy_inode,
  2128. #ifdef CONFIG_TMPFS
  2129. .statfs = shmem_statfs,
  2130. .remount_fs = shmem_remount_fs,
  2131. .show_options = shmem_show_options,
  2132. #endif
  2133. .evict_inode = shmem_evict_inode,
  2134. .drop_inode = generic_delete_inode,
  2135. .put_super = shmem_put_super,
  2136. };
  2137. static const struct vm_operations_struct shmem_vm_ops = {
  2138. .fault = shmem_fault,
  2139. #ifdef CONFIG_NUMA
  2140. .set_policy = shmem_set_policy,
  2141. .get_policy = shmem_get_policy,
  2142. #endif
  2143. };
  2144. static struct dentry *shmem_mount(struct file_system_type *fs_type,
  2145. int flags, const char *dev_name, void *data)
  2146. {
  2147. return mount_nodev(fs_type, flags, data, shmem_fill_super);
  2148. }
  2149. static struct file_system_type shmem_fs_type = {
  2150. .owner = THIS_MODULE,
  2151. .name = "tmpfs",
  2152. .mount = shmem_mount,
  2153. .kill_sb = kill_litter_super,
  2154. };
  2155. int __init shmem_init(void)
  2156. {
  2157. int error;
  2158. error = bdi_init(&shmem_backing_dev_info);
  2159. if (error)
  2160. goto out4;
  2161. error = shmem_init_inodecache();
  2162. if (error)
  2163. goto out3;
  2164. error = register_filesystem(&shmem_fs_type);
  2165. if (error) {
  2166. printk(KERN_ERR "Could not register tmpfs\n");
  2167. goto out2;
  2168. }
  2169. shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
  2170. shmem_fs_type.name, NULL);
  2171. if (IS_ERR(shm_mnt)) {
  2172. error = PTR_ERR(shm_mnt);
  2173. printk(KERN_ERR "Could not kern_mount tmpfs\n");
  2174. goto out1;
  2175. }
  2176. return 0;
  2177. out1:
  2178. unregister_filesystem(&shmem_fs_type);
  2179. out2:
  2180. shmem_destroy_inodecache();
  2181. out3:
  2182. bdi_destroy(&shmem_backing_dev_info);
  2183. out4:
  2184. shm_mnt = ERR_PTR(error);
  2185. return error;
  2186. }
  2187. #else /* !CONFIG_SHMEM */
  2188. /*
  2189. * tiny-shmem: simple shmemfs and tmpfs using ramfs code
  2190. *
  2191. * This is intended for small system where the benefits of the full
  2192. * shmem code (swap-backed and resource-limited) are outweighed by
  2193. * their complexity. On systems without swap this code should be
  2194. * effectively equivalent, but much lighter weight.
  2195. */
  2196. #include <linux/ramfs.h>
  2197. static struct file_system_type shmem_fs_type = {
  2198. .name = "tmpfs",
  2199. .mount = ramfs_mount,
  2200. .kill_sb = kill_litter_super,
  2201. };
  2202. int __init shmem_init(void)
  2203. {
  2204. BUG_ON(register_filesystem(&shmem_fs_type) != 0);
  2205. shm_mnt = kern_mount(&shmem_fs_type);
  2206. BUG_ON(IS_ERR(shm_mnt));
  2207. return 0;
  2208. }
  2209. int shmem_unuse(swp_entry_t swap, struct page *page)
  2210. {
  2211. return 0;
  2212. }
  2213. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  2214. {
  2215. return 0;
  2216. }
  2217. void shmem_unlock_mapping(struct address_space *mapping)
  2218. {
  2219. }
  2220. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  2221. {
  2222. truncate_inode_pages_range(inode->i_mapping, lstart, lend);
  2223. }
  2224. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  2225. #define shmem_vm_ops generic_file_vm_ops
  2226. #define shmem_file_operations ramfs_file_operations
  2227. #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
  2228. #define shmem_acct_size(flags, size) 0
  2229. #define shmem_unacct_size(flags, size) do {} while (0)
  2230. #endif /* CONFIG_SHMEM */
  2231. /* common code */
  2232. /**
  2233. * shmem_file_setup - get an unlinked file living in tmpfs
  2234. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2235. * @size: size to be set for the file
  2236. * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
  2237. */
  2238. struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
  2239. {
  2240. int error;
  2241. struct file *file;
  2242. struct inode *inode;
  2243. struct path path;
  2244. struct dentry *root;
  2245. struct qstr this;
  2246. if (IS_ERR(shm_mnt))
  2247. return (void *)shm_mnt;
  2248. if (size < 0 || size > MAX_LFS_FILESIZE)
  2249. return ERR_PTR(-EINVAL);
  2250. if (shmem_acct_size(flags, size))
  2251. return ERR_PTR(-ENOMEM);
  2252. error = -ENOMEM;
  2253. this.name = name;
  2254. this.len = strlen(name);
  2255. this.hash = 0; /* will go */
  2256. root = shm_mnt->mnt_root;
  2257. path.dentry = d_alloc(root, &this);
  2258. if (!path.dentry)
  2259. goto put_memory;
  2260. path.mnt = mntget(shm_mnt);
  2261. error = -ENOSPC;
  2262. inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
  2263. if (!inode)
  2264. goto put_dentry;
  2265. d_instantiate(path.dentry, inode);
  2266. inode->i_size = size;
  2267. clear_nlink(inode); /* It is unlinked */
  2268. #ifndef CONFIG_MMU
  2269. error = ramfs_nommu_expand_for_mapping(inode, size);
  2270. if (error)
  2271. goto put_dentry;
  2272. #endif
  2273. error = -ENFILE;
  2274. file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  2275. &shmem_file_operations);
  2276. if (!file)
  2277. goto put_dentry;
  2278. return file;
  2279. put_dentry:
  2280. path_put(&path);
  2281. put_memory:
  2282. shmem_unacct_size(flags, size);
  2283. return ERR_PTR(error);
  2284. }
  2285. EXPORT_SYMBOL_GPL(shmem_file_setup);
  2286. /**
  2287. * shmem_zero_setup - setup a shared anonymous mapping
  2288. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  2289. */
  2290. int shmem_zero_setup(struct vm_area_struct *vma)
  2291. {
  2292. struct file *file;
  2293. loff_t size = vma->vm_end - vma->vm_start;
  2294. file = shmem_file_setup("dev/zero", size, vma->vm_flags);
  2295. if (IS_ERR(file))
  2296. return PTR_ERR(file);
  2297. if (vma->vm_file)
  2298. fput(vma->vm_file);
  2299. vma->vm_file = file;
  2300. vma->vm_ops = &shmem_vm_ops;
  2301. vma->vm_flags |= VM_CAN_NONLINEAR;
  2302. return 0;
  2303. }
  2304. /**
  2305. * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
  2306. * @mapping: the page's address_space
  2307. * @index: the page index
  2308. * @gfp: the page allocator flags to use if allocating
  2309. *
  2310. * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
  2311. * with any new page allocations done using the specified allocation flags.
  2312. * But read_cache_page_gfp() uses the ->readpage() method: which does not
  2313. * suit tmpfs, since it may have pages in swapcache, and needs to find those
  2314. * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
  2315. *
  2316. * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
  2317. * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
  2318. */
  2319. struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
  2320. pgoff_t index, gfp_t gfp)
  2321. {
  2322. #ifdef CONFIG_SHMEM
  2323. struct inode *inode = mapping->host;
  2324. struct page *page;
  2325. int error;
  2326. BUG_ON(mapping->a_ops != &shmem_aops);
  2327. error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
  2328. if (error)
  2329. page = ERR_PTR(error);
  2330. else
  2331. unlock_page(page);
  2332. return page;
  2333. #else
  2334. /*
  2335. * The tiny !SHMEM case uses ramfs without swap
  2336. */
  2337. return read_cache_page_gfp(mapping, index, gfp);
  2338. #endif
  2339. }
  2340. EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);