shmem.c 68 KB

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