shmem.c 65 KB

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