shmem.c 64 KB

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