Skip to content
Snippets Groups Projects
nfs4xdr.c 109 KiB
Newer Older
  • Learn to ignore specific revisions
  • Linus Torvalds's avatar
    Linus Torvalds committed
    2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885
     *   These may look a little ugly since they are imported from a "generic"
     * set of XDR encode/decode routines which are intended to be shared by
     * all of our NFSv4 implementations (OpenBSD, MacOS X...).
     *
     * If the pain of reading these is too great, it should be a straightforward
     * task to translate them into Linux-specific versions which are more
     * consistent with the style used in NFSv2/v3...
     */
    #define READ32(x)         (x) = ntohl(*p++)
    #define READ64(x)         do {			\
    	(x) = (u64)ntohl(*p++) << 32;		\
    	(x) |= ntohl(*p++);			\
    } while (0)
    #define READTIME(x)       do {			\
    	p++;					\
    	(x.tv_sec) = ntohl(*p++);		\
    	(x.tv_nsec) = ntohl(*p++);		\
    } while (0)
    #define COPYMEM(x,nbytes) do {			\
    	memcpy((x), p, nbytes);			\
    	p += XDR_QUADLEN(nbytes);		\
    } while (0)
    
    #define READ_BUF(nbytes)  do { \
    	p = xdr_inline_decode(xdr, nbytes); \
    	if (!p) { \
    		printk(KERN_WARNING "%s: reply buffer overflowed in line %d.", \
    			       	__FUNCTION__, __LINE__); \
    		return -EIO; \
    	} \
    } while (0)
    
    static int decode_opaque_inline(struct xdr_stream *xdr, uint32_t *len, char **string)
    {
    	uint32_t *p;
    
    	READ_BUF(4);
    	READ32(*len);
    	READ_BUF(*len);
    	*string = (char *)p;
    	return 0;
    }
    
    static int decode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
    {
    	uint32_t *p;
    
    	READ_BUF(8);
    	READ32(hdr->status);
    	READ32(hdr->taglen);
    	
    	READ_BUF(hdr->taglen + 4);
    	hdr->tag = (char *)p;
    	p += XDR_QUADLEN(hdr->taglen);
    	READ32(hdr->nops);
    	return 0;
    }
    
    static int decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
    {
    	uint32_t *p;
    	uint32_t opnum;
    	int32_t nfserr;
    
    	READ_BUF(8);
    	READ32(opnum);
    	if (opnum != expected) {
    		printk(KERN_NOTICE
    				"nfs4_decode_op_hdr: Server returned operation"
    			       	" %d but we issued a request for %d\n",
    				opnum, expected);
    		return -EIO;
    	}
    	READ32(nfserr);
    	if (nfserr != NFS_OK)
    		return -nfs_stat_to_errno(nfserr);
    	return 0;
    }
    
    /* Dummy routine */
    static int decode_ace(struct xdr_stream *xdr, void *ace, struct nfs4_client *clp)
    {
    	uint32_t *p;
    	uint32_t strlen;
    	char *str;
    
    	READ_BUF(12);
    	return decode_opaque_inline(xdr, &strlen, &str);
    }
    
    static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
    {
    	uint32_t bmlen, *p;
    
    	READ_BUF(4);
    	READ32(bmlen);
    
    	bitmap[0] = bitmap[1] = 0;
    	READ_BUF((bmlen << 2));
    	if (bmlen > 0) {
    		READ32(bitmap[0]);
    		if (bmlen > 1)
    			READ32(bitmap[1]);
    	}
    	return 0;
    }
    
    static inline int decode_attr_length(struct xdr_stream *xdr, uint32_t *attrlen, uint32_t **savep)
    {
    	uint32_t *p;
    
    	READ_BUF(4);
    	READ32(*attrlen);
    	*savep = xdr->p;
    	return 0;
    }
    
    static int decode_attr_supported(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *bitmask)
    {
    	if (likely(bitmap[0] & FATTR4_WORD0_SUPPORTED_ATTRS)) {
    		decode_attr_bitmap(xdr, bitmask);
    		bitmap[0] &= ~FATTR4_WORD0_SUPPORTED_ATTRS;
    	} else
    		bitmask[0] = bitmask[1] = 0;
    	dprintk("%s: bitmask=0x%x%x\n", __FUNCTION__, bitmask[0], bitmask[1]);
    	return 0;
    }
    
    static int decode_attr_type(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *type)
    {
    	uint32_t *p;
    
    	*type = 0;
    	if (unlikely(bitmap[0] & (FATTR4_WORD0_TYPE - 1U)))
    		return -EIO;
    	if (likely(bitmap[0] & FATTR4_WORD0_TYPE)) {
    		READ_BUF(4);
    		READ32(*type);
    		if (*type < NF4REG || *type > NF4NAMEDATTR) {
    			dprintk("%s: bad type %d\n", __FUNCTION__, *type);
    			return -EIO;
    		}
    		bitmap[0] &= ~FATTR4_WORD0_TYPE;
    	}
    	dprintk("%s: type=0%o\n", __FUNCTION__, nfs_type2fmt[*type].nfs2type);
    	return 0;
    }
    
    static int decode_attr_change(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *change)
    {
    	uint32_t *p;
    
    	*change = 0;
    	if (unlikely(bitmap[0] & (FATTR4_WORD0_CHANGE - 1U)))
    		return -EIO;
    	if (likely(bitmap[0] & FATTR4_WORD0_CHANGE)) {
    		READ_BUF(8);
    		READ64(*change);
    		bitmap[0] &= ~FATTR4_WORD0_CHANGE;
    	}
    	dprintk("%s: change attribute=%Lu\n", __FUNCTION__,
    			(unsigned long long)*change);
    	return 0;
    }
    
    static int decode_attr_size(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *size)
    {
    	uint32_t *p;
    
    	*size = 0;
    	if (unlikely(bitmap[0] & (FATTR4_WORD0_SIZE - 1U)))
    		return -EIO;
    	if (likely(bitmap[0] & FATTR4_WORD0_SIZE)) {
    		READ_BUF(8);
    		READ64(*size);
    		bitmap[0] &= ~FATTR4_WORD0_SIZE;
    	}
    	dprintk("%s: file size=%Lu\n", __FUNCTION__, (unsigned long long)*size);
    	return 0;
    }
    
    static int decode_attr_link_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
    {
    	uint32_t *p;
    
    	*res = 0;
    	if (unlikely(bitmap[0] & (FATTR4_WORD0_LINK_SUPPORT - 1U)))
    		return -EIO;
    	if (likely(bitmap[0] & FATTR4_WORD0_LINK_SUPPORT)) {
    		READ_BUF(4);
    		READ32(*res);
    		bitmap[0] &= ~FATTR4_WORD0_LINK_SUPPORT;
    	}
    	dprintk("%s: link support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
    	return 0;
    }
    
    static int decode_attr_symlink_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
    {
    	uint32_t *p;
    
    	*res = 0;
    	if (unlikely(bitmap[0] & (FATTR4_WORD0_SYMLINK_SUPPORT - 1U)))
    		return -EIO;
    	if (likely(bitmap[0] & FATTR4_WORD0_SYMLINK_SUPPORT)) {
    		READ_BUF(4);
    		READ32(*res);
    		bitmap[0] &= ~FATTR4_WORD0_SYMLINK_SUPPORT;
    	}
    	dprintk("%s: symlink support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
    	return 0;
    }
    
    static int decode_attr_fsid(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_fsid *fsid)
    {
    	uint32_t *p;
    
    	fsid->major = 0;
    	fsid->minor = 0;
    	if (unlikely(bitmap[0] & (FATTR4_WORD0_FSID - 1U)))
    		return -EIO;
    	if (likely(bitmap[0] & FATTR4_WORD0_FSID)) {
    		READ_BUF(16);
    		READ64(fsid->major);
    		READ64(fsid->minor);
    		bitmap[0] &= ~FATTR4_WORD0_FSID;
    	}
    	dprintk("%s: fsid=(0x%Lx/0x%Lx)\n", __FUNCTION__,
    			(unsigned long long)fsid->major,
    			(unsigned long long)fsid->minor);
    	return 0;
    }
    
    static int decode_attr_lease_time(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
    {
    	uint32_t *p;
    
    	*res = 60;
    	if (unlikely(bitmap[0] & (FATTR4_WORD0_LEASE_TIME - 1U)))
    		return -EIO;
    	if (likely(bitmap[0] & FATTR4_WORD0_LEASE_TIME)) {
    		READ_BUF(4);
    		READ32(*res);
    		bitmap[0] &= ~FATTR4_WORD0_LEASE_TIME;
    	}
    	dprintk("%s: file size=%u\n", __FUNCTION__, (unsigned int)*res);
    	return 0;
    }
    
    static int decode_attr_aclsupport(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
    {
    	uint32_t *p;
    
    	*res = ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL;
    	if (unlikely(bitmap[0] & (FATTR4_WORD0_ACLSUPPORT - 1U)))
    		return -EIO;
    	if (likely(bitmap[0] & FATTR4_WORD0_ACLSUPPORT)) {
    		READ_BUF(4);
    		READ32(*res);
    		bitmap[0] &= ~FATTR4_WORD0_ACLSUPPORT;
    	}
    	dprintk("%s: ACLs supported=%u\n", __FUNCTION__, (unsigned int)*res);
    	return 0;
    }
    
    static int decode_attr_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
    {
    	uint32_t *p;
    
    	*fileid = 0;
    	if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEID - 1U)))
    		return -EIO;
    	if (likely(bitmap[0] & FATTR4_WORD0_FILEID)) {
    		READ_BUF(8);
    		READ64(*fileid);
    		bitmap[0] &= ~FATTR4_WORD0_FILEID;
    	}
    	dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
    	return 0;
    }
    
    static int decode_attr_files_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
    {
    	uint32_t *p;
    	int status = 0;
    
    	*res = 0;
    	if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_AVAIL - 1U)))
    		return -EIO;
    	if (likely(bitmap[0] & FATTR4_WORD0_FILES_AVAIL)) {
    		READ_BUF(8);
    		READ64(*res);
    		bitmap[0] &= ~FATTR4_WORD0_FILES_AVAIL;
    	}
    	dprintk("%s: files avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
    	return status;
    }
    
    static int decode_attr_files_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
    {
    	uint32_t *p;
    	int status = 0;
    
    	*res = 0;
    	if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_FREE - 1U)))
    		return -EIO;
    	if (likely(bitmap[0] & FATTR4_WORD0_FILES_FREE)) {
    		READ_BUF(8);
    		READ64(*res);
    		bitmap[0] &= ~FATTR4_WORD0_FILES_FREE;
    	}
    	dprintk("%s: files free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
    	return status;
    }
    
    static int decode_attr_files_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
    {
    	uint32_t *p;
    	int status = 0;
    
    	*res = 0;
    	if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_TOTAL - 1U)))
    		return -EIO;
    	if (likely(bitmap[0] & FATTR4_WORD0_FILES_TOTAL)) {
    		READ_BUF(8);
    		READ64(*res);
    		bitmap[0] &= ~FATTR4_WORD0_FILES_TOTAL;
    	}
    	dprintk("%s: files total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
    	return status;
    }
    
    static int decode_attr_maxfilesize(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
    {
    	uint32_t *p;
    	int status = 0;
    
    	*res = 0;
    	if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXFILESIZE - 1U)))
    		return -EIO;
    	if (likely(bitmap[0] & FATTR4_WORD0_MAXFILESIZE)) {
    		READ_BUF(8);
    		READ64(*res);
    		bitmap[0] &= ~FATTR4_WORD0_MAXFILESIZE;
    	}
    	dprintk("%s: maxfilesize=%Lu\n", __FUNCTION__, (unsigned long long)*res);
    	return status;
    }
    
    static int decode_attr_maxlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxlink)
    {
    	uint32_t *p;
    	int status = 0;
    
    	*maxlink = 1;
    	if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXLINK - 1U)))
    		return -EIO;
    	if (likely(bitmap[0] & FATTR4_WORD0_MAXLINK)) {
    		READ_BUF(4);
    		READ32(*maxlink);
    		bitmap[0] &= ~FATTR4_WORD0_MAXLINK;
    	}
    	dprintk("%s: maxlink=%u\n", __FUNCTION__, *maxlink);
    	return status;
    }
    
    static int decode_attr_maxname(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxname)
    {
    	uint32_t *p;
    	int status = 0;
    
    	*maxname = 1024;
    	if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXNAME - 1U)))
    		return -EIO;
    	if (likely(bitmap[0] & FATTR4_WORD0_MAXNAME)) {
    		READ_BUF(4);
    		READ32(*maxname);
    		bitmap[0] &= ~FATTR4_WORD0_MAXNAME;
    	}
    	dprintk("%s: maxname=%u\n", __FUNCTION__, *maxname);
    	return status;
    }
    
    static int decode_attr_maxread(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
    {
    	uint32_t *p;
    	int status = 0;
    
    	*res = 1024;
    	if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXREAD - 1U)))
    		return -EIO;
    	if (likely(bitmap[0] & FATTR4_WORD0_MAXREAD)) {
    		uint64_t maxread;
    		READ_BUF(8);
    		READ64(maxread);
    		if (maxread > 0x7FFFFFFF)
    			maxread = 0x7FFFFFFF;
    		*res = (uint32_t)maxread;
    		bitmap[0] &= ~FATTR4_WORD0_MAXREAD;
    	}
    	dprintk("%s: maxread=%lu\n", __FUNCTION__, (unsigned long)*res);
    	return status;
    }
    
    static int decode_attr_maxwrite(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
    {
    	uint32_t *p;
    	int status = 0;
    
    	*res = 1024;
    	if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXWRITE - 1U)))
    		return -EIO;
    	if (likely(bitmap[0] & FATTR4_WORD0_MAXWRITE)) {
    		uint64_t maxwrite;
    		READ_BUF(8);
    		READ64(maxwrite);
    		if (maxwrite > 0x7FFFFFFF)
    			maxwrite = 0x7FFFFFFF;
    		*res = (uint32_t)maxwrite;
    		bitmap[0] &= ~FATTR4_WORD0_MAXWRITE;
    	}
    	dprintk("%s: maxwrite=%lu\n", __FUNCTION__, (unsigned long)*res);
    	return status;
    }
    
    static int decode_attr_mode(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *mode)
    {
    	uint32_t *p;
    
    	*mode = 0;
    	if (unlikely(bitmap[1] & (FATTR4_WORD1_MODE - 1U)))
    		return -EIO;
    	if (likely(bitmap[1] & FATTR4_WORD1_MODE)) {
    		READ_BUF(4);
    		READ32(*mode);
    		*mode &= ~S_IFMT;
    		bitmap[1] &= ~FATTR4_WORD1_MODE;
    	}
    	dprintk("%s: file mode=0%o\n", __FUNCTION__, (unsigned int)*mode);
    	return 0;
    }
    
    static int decode_attr_nlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *nlink)
    {
    	uint32_t *p;
    
    	*nlink = 1;
    	if (unlikely(bitmap[1] & (FATTR4_WORD1_NUMLINKS - 1U)))
    		return -EIO;
    	if (likely(bitmap[1] & FATTR4_WORD1_NUMLINKS)) {
    		READ_BUF(4);
    		READ32(*nlink);
    		bitmap[1] &= ~FATTR4_WORD1_NUMLINKS;
    	}
    	dprintk("%s: nlink=%u\n", __FUNCTION__, (unsigned int)*nlink);
    	return 0;
    }
    
    static int decode_attr_owner(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_client *clp, int32_t *uid)
    {
    	uint32_t len, *p;
    
    	*uid = -2;
    	if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER - 1U)))
    		return -EIO;
    	if (likely(bitmap[1] & FATTR4_WORD1_OWNER)) {
    		READ_BUF(4);
    		READ32(len);
    		READ_BUF(len);
    		if (len < XDR_MAX_NETOBJ) {
    			if (nfs_map_name_to_uid(clp, (char *)p, len, uid) != 0)
    				dprintk("%s: nfs_map_name_to_uid failed!\n",
    						__FUNCTION__);
    		} else
    			printk(KERN_WARNING "%s: name too long (%u)!\n",
    					__FUNCTION__, len);
    		bitmap[1] &= ~FATTR4_WORD1_OWNER;
    	}
    	dprintk("%s: uid=%d\n", __FUNCTION__, (int)*uid);
    	return 0;
    }
    
    static int decode_attr_group(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_client *clp, int32_t *gid)
    {
    	uint32_t len, *p;
    
    	*gid = -2;
    	if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER_GROUP - 1U)))
    		return -EIO;
    	if (likely(bitmap[1] & FATTR4_WORD1_OWNER_GROUP)) {
    		READ_BUF(4);
    		READ32(len);
    		READ_BUF(len);
    		if (len < XDR_MAX_NETOBJ) {
    			if (nfs_map_group_to_gid(clp, (char *)p, len, gid) != 0)
    				dprintk("%s: nfs_map_group_to_gid failed!\n",
    						__FUNCTION__);
    		} else
    			printk(KERN_WARNING "%s: name too long (%u)!\n",
    					__FUNCTION__, len);
    		bitmap[1] &= ~FATTR4_WORD1_OWNER_GROUP;
    	}
    	dprintk("%s: gid=%d\n", __FUNCTION__, (int)*gid);
    	return 0;
    }
    
    static int decode_attr_rdev(struct xdr_stream *xdr, uint32_t *bitmap, dev_t *rdev)
    {
    	uint32_t major = 0, minor = 0, *p;
    
    	*rdev = MKDEV(0,0);
    	if (unlikely(bitmap[1] & (FATTR4_WORD1_RAWDEV - 1U)))
    		return -EIO;
    	if (likely(bitmap[1] & FATTR4_WORD1_RAWDEV)) {
    		dev_t tmp;
    
    		READ_BUF(8);
    		READ32(major);
    		READ32(minor);
    		tmp = MKDEV(major, minor);
    		if (MAJOR(tmp) == major && MINOR(tmp) == minor)
    			*rdev = tmp;
    		bitmap[1] &= ~ FATTR4_WORD1_RAWDEV;
    	}
    	dprintk("%s: rdev=(0x%x:0x%x)\n", __FUNCTION__, major, minor);
    	return 0;
    }
    
    static int decode_attr_space_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
    {
    	uint32_t *p;
    	int status = 0;
    
    	*res = 0;
    	if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_AVAIL - 1U)))
    		return -EIO;
    	if (likely(bitmap[1] & FATTR4_WORD1_SPACE_AVAIL)) {
    		READ_BUF(8);
    		READ64(*res);
    		bitmap[1] &= ~FATTR4_WORD1_SPACE_AVAIL;
    	}
    	dprintk("%s: space avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
    	return status;
    }
    
    static int decode_attr_space_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
    {
    	uint32_t *p;
    	int status = 0;
    
    	*res = 0;
    	if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_FREE - 1U)))
    		return -EIO;
    	if (likely(bitmap[1] & FATTR4_WORD1_SPACE_FREE)) {
    		READ_BUF(8);
    		READ64(*res);
    		bitmap[1] &= ~FATTR4_WORD1_SPACE_FREE;
    	}
    	dprintk("%s: space free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
    	return status;
    }
    
    static int decode_attr_space_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
    {
    	uint32_t *p;
    	int status = 0;
    
    	*res = 0;
    	if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_TOTAL - 1U)))
    		return -EIO;
    	if (likely(bitmap[1] & FATTR4_WORD1_SPACE_TOTAL)) {
    		READ_BUF(8);
    		READ64(*res);
    		bitmap[1] &= ~FATTR4_WORD1_SPACE_TOTAL;
    	}
    	dprintk("%s: space total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
    	return status;
    }
    
    static int decode_attr_space_used(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *used)
    {
    	uint32_t *p;
    
    	*used = 0;
    	if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_USED - 1U)))
    		return -EIO;
    	if (likely(bitmap[1] & FATTR4_WORD1_SPACE_USED)) {
    		READ_BUF(8);
    		READ64(*used);
    		bitmap[1] &= ~FATTR4_WORD1_SPACE_USED;
    	}
    	dprintk("%s: space used=%Lu\n", __FUNCTION__,
    			(unsigned long long)*used);
    	return 0;
    }
    
    static int decode_attr_time(struct xdr_stream *xdr, struct timespec *time)
    {
    	uint32_t *p;
    	uint64_t sec;
    	uint32_t nsec;
    
    	READ_BUF(12);
    	READ64(sec);
    	READ32(nsec);
    	time->tv_sec = (time_t)sec;
    	time->tv_nsec = (long)nsec;
    	return 0;
    }
    
    static int decode_attr_time_access(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
    {
    	int status = 0;
    
    	time->tv_sec = 0;
    	time->tv_nsec = 0;
    	if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_ACCESS - 1U)))
    		return -EIO;
    	if (likely(bitmap[1] & FATTR4_WORD1_TIME_ACCESS)) {
    		status = decode_attr_time(xdr, time);
    		bitmap[1] &= ~FATTR4_WORD1_TIME_ACCESS;
    	}
    	dprintk("%s: atime=%ld\n", __FUNCTION__, (long)time->tv_sec);
    	return status;
    }
    
    static int decode_attr_time_metadata(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
    {
    	int status = 0;
    
    	time->tv_sec = 0;
    	time->tv_nsec = 0;
    	if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_METADATA - 1U)))
    		return -EIO;
    	if (likely(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) {
    		status = decode_attr_time(xdr, time);
    		bitmap[1] &= ~FATTR4_WORD1_TIME_METADATA;
    	}
    	dprintk("%s: ctime=%ld\n", __FUNCTION__, (long)time->tv_sec);
    	return status;
    }
    
    static int decode_attr_time_modify(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
    {
    	int status = 0;
    
    	time->tv_sec = 0;
    	time->tv_nsec = 0;
    	if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_MODIFY - 1U)))
    		return -EIO;
    	if (likely(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) {
    		status = decode_attr_time(xdr, time);
    		bitmap[1] &= ~FATTR4_WORD1_TIME_MODIFY;
    	}
    	dprintk("%s: mtime=%ld\n", __FUNCTION__, (long)time->tv_sec);
    	return status;
    }
    
    static int verify_attr_len(struct xdr_stream *xdr, uint32_t *savep, uint32_t attrlen)
    {
    	unsigned int attrwords = XDR_QUADLEN(attrlen);
    	unsigned int nwords = xdr->p - savep;
    
    	if (unlikely(attrwords != nwords)) {
    		printk(KERN_WARNING "%s: server returned incorrect attribute length: %u %c %u\n",
    				__FUNCTION__,
    				attrwords << 2,
    				(attrwords < nwords) ? '<' : '>',
    				nwords << 2);
    		return -EIO;
    	}
    	return 0;
    }
    
    static int decode_change_info(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
    {
    	uint32_t *p;
    
    	READ_BUF(20);
    	READ32(cinfo->atomic);
    	READ64(cinfo->before);
    	READ64(cinfo->after);
    	return 0;
    }
    
    static int decode_access(struct xdr_stream *xdr, struct nfs4_accessres *access)
    {
    	uint32_t *p;
    	uint32_t supp, acc;
    	int status;
    
    	status = decode_op_hdr(xdr, OP_ACCESS);
    	if (status)
    		return status;
    	READ_BUF(8);
    	READ32(supp);
    	READ32(acc);
    	access->supported = supp;
    	access->access = acc;
    	return 0;
    }
    
    static int decode_close(struct xdr_stream *xdr, struct nfs_closeres *res)
    {
    	uint32_t *p;
    	int status;
    
    	status = decode_op_hdr(xdr, OP_CLOSE);
    	if (status)
    		return status;
    	READ_BUF(sizeof(res->stateid.data));
    	COPYMEM(res->stateid.data, sizeof(res->stateid.data));
    	return 0;
    }
    
    static int decode_commit(struct xdr_stream *xdr, struct nfs_writeres *res)
    {
    	uint32_t *p;
    	int status;
    
    	status = decode_op_hdr(xdr, OP_COMMIT);
    	if (status)
    		return status;
    	READ_BUF(8);
    	COPYMEM(res->verf->verifier, 8);
    	return 0;
    }
    
    static int decode_create(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
    {
    	uint32_t *p;
    	uint32_t bmlen;
    	int status;
    
    	status = decode_op_hdr(xdr, OP_CREATE);
    	if (status)
    		return status;
    	if ((status = decode_change_info(xdr, cinfo)))
    		return status;
    	READ_BUF(4);
    	READ32(bmlen);
    	READ_BUF(bmlen << 2);
    	return 0;
    }
    
    static int decode_server_caps(struct xdr_stream *xdr, struct nfs4_server_caps_res *res)
    {
    	uint32_t *savep;
    	uint32_t attrlen, 
    		 bitmap[2] = {0};
    	int status;
    
    	if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_supported(xdr, bitmap, res->attr_bitmask)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_link_support(xdr, bitmap, &res->has_links)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_symlink_support(xdr, bitmap, &res->has_symlinks)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_aclsupport(xdr, bitmap, &res->acl_bitmask)) != 0)
    		goto xdr_error;
    	status = verify_attr_len(xdr, savep, attrlen);
    xdr_error:
    	if (status != 0)
    		printk(KERN_NOTICE "%s: xdr error %d!\n", __FUNCTION__, -status);
    	return status;
    }
    	
    static int decode_statfs(struct xdr_stream *xdr, struct nfs_fsstat *fsstat)
    {
    	uint32_t *savep;
    	uint32_t attrlen, 
    		 bitmap[2] = {0};
    	int status;
    	
    	if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
    		goto xdr_error;
    
    	if ((status = decode_attr_files_avail(xdr, bitmap, &fsstat->afiles)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_files_free(xdr, bitmap, &fsstat->ffiles)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_files_total(xdr, bitmap, &fsstat->tfiles)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_space_avail(xdr, bitmap, &fsstat->abytes)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_space_free(xdr, bitmap, &fsstat->fbytes)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_space_total(xdr, bitmap, &fsstat->tbytes)) != 0)
    		goto xdr_error;
    
    	status = verify_attr_len(xdr, savep, attrlen);
    xdr_error:
    	if (status != 0)
    		printk(KERN_NOTICE "%s: xdr error %d!\n", __FUNCTION__, -status);
    	return status;
    }
    
    static int decode_pathconf(struct xdr_stream *xdr, struct nfs_pathconf *pathconf)
    {
    	uint32_t *savep;
    	uint32_t attrlen, 
    		 bitmap[2] = {0};
    	int status;
    	
    	if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
    		goto xdr_error;
    
    	if ((status = decode_attr_maxlink(xdr, bitmap, &pathconf->max_link)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_maxname(xdr, bitmap, &pathconf->max_namelen)) != 0)
    		goto xdr_error;
    
    	status = verify_attr_len(xdr, savep, attrlen);
    xdr_error:
    	if (status != 0)
    		printk(KERN_NOTICE "%s: xdr error %d!\n", __FUNCTION__, -status);
    	return status;
    }
    
    static int decode_getfattr(struct xdr_stream *xdr, struct nfs_fattr *fattr, const struct nfs_server *server)
    {
    	uint32_t *savep;
    	uint32_t attrlen,
    		 bitmap[2] = {0},
    		 type;
    	int status, fmode = 0;
    
    	if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
    		goto xdr_error;
    
    	fattr->bitmap[0] = bitmap[0];
    	fattr->bitmap[1] = bitmap[1];
    
    	if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
    		goto xdr_error;
    
    
    	if ((status = decode_attr_type(xdr, bitmap, &type)) != 0)
    		goto xdr_error;
    	fattr->type = nfs_type2fmt[type].nfs2type;
    	fmode = nfs_type2fmt[type].mode;
    
    	if ((status = decode_attr_change(xdr, bitmap, &fattr->change_attr)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_size(xdr, bitmap, &fattr->size)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_fsid(xdr, bitmap, &fattr->fsid_u.nfs4)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_fileid(xdr, bitmap, &fattr->fileid)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_mode(xdr, bitmap, &fattr->mode)) != 0)
    		goto xdr_error;
    	fattr->mode |= fmode;
    	if ((status = decode_attr_nlink(xdr, bitmap, &fattr->nlink)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_owner(xdr, bitmap, server->nfs4_state, &fattr->uid)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_group(xdr, bitmap, server->nfs4_state, &fattr->gid)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_rdev(xdr, bitmap, &fattr->rdev)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_space_used(xdr, bitmap, &fattr->du.nfs3.used)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_time_access(xdr, bitmap, &fattr->atime)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_time_metadata(xdr, bitmap, &fattr->ctime)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_time_modify(xdr, bitmap, &fattr->mtime)) != 0)
    		goto xdr_error;
    
    	if ((status = verify_attr_len(xdr, savep, attrlen)) == 0)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		fattr->valid = NFS_ATTR_FATTR | NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4;
    xdr_error:
    	if (status != 0)
    		printk(KERN_NOTICE "%s: xdr error %d!\n", __FUNCTION__, -status);
    	return status;
    }
    
    
    static int decode_fsinfo(struct xdr_stream *xdr, struct nfs_fsinfo *fsinfo)
    {
    	uint32_t *savep;
    	uint32_t attrlen, bitmap[2];
    	int status;
    
    	if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
    		goto xdr_error;
    
    	fsinfo->rtmult = fsinfo->wtmult = 512;	/* ??? */
    
    	if ((status = decode_attr_lease_time(xdr, bitmap, &fsinfo->lease_time)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_maxfilesize(xdr, bitmap, &fsinfo->maxfilesize)) != 0)
    		goto xdr_error;
    	if ((status = decode_attr_maxread(xdr, bitmap, &fsinfo->rtmax)) != 0)
    		goto xdr_error;
    	fsinfo->rtpref = fsinfo->dtpref = fsinfo->rtmax;
    	if ((status = decode_attr_maxwrite(xdr, bitmap, &fsinfo->wtmax)) != 0)
    		goto xdr_error;
    	fsinfo->wtpref = fsinfo->wtmax;
    
    	status = verify_attr_len(xdr, savep, attrlen);
    xdr_error:
    	if (status != 0)
    		printk(KERN_NOTICE "%s: xdr error %d!\n", __FUNCTION__, -status);
    	return status;
    }
    
    static int decode_getfh(struct xdr_stream *xdr, struct nfs_fh *fh)
    {
    	uint32_t *p;
    	uint32_t len;
    	int status;
    
    	status = decode_op_hdr(xdr, OP_GETFH);
    	if (status)
    		return status;
    	/* Zero handle first to allow comparisons */
    	memset(fh, 0, sizeof(*fh));
    
    	READ_BUF(4);
    	READ32(len);
    	if (len > NFS4_FHSIZE)
    		return -EIO;
    	fh->size = len;
    	READ_BUF(len);
    	COPYMEM(fh->data, len);
    	return 0;
    }
    
    static int decode_link(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
    {
    	int status;
    	
    	status = decode_op_hdr(xdr, OP_LINK);
    	if (status)
    		return status;
    	return decode_change_info(xdr, cinfo);
    }
    
    /*
     * We create the owner, so we know a proper owner.id length is 4.
     */
    static int decode_lock_denied (struct xdr_stream *xdr, struct nfs_lock_denied *denied)
    {
    	uint32_t *p;
    	uint32_t namelen;
    
    	READ_BUF(32);
    	READ64(denied->offset);
    	READ64(denied->length);
    	READ32(denied->type);
    	READ64(denied->owner.clientid);
    	READ32(namelen);
    	READ_BUF(namelen);
    	if (namelen == 4)
    		READ32(denied->owner.id);
    	return -NFS4ERR_DENIED;
    }
    
    static int decode_lock(struct xdr_stream *xdr, struct nfs_lockres *res)
    {
    	uint32_t *p;
    	int status;
    
    	status = decode_op_hdr(xdr, OP_LOCK);
    	if (status == 0) {
    
    		READ_BUF(sizeof(res->u.stateid.data));
    		COPYMEM(res->u.stateid.data, sizeof(res->u.stateid.data));
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	} else if (status == -NFS4ERR_DENIED)
    		return decode_lock_denied(xdr, &res->u.denied);
    	return status;
    }
    
    static int decode_lockt(struct xdr_stream *xdr, struct nfs_lockres *res)
    {
    	int status;
    	status = decode_op_hdr(xdr, OP_LOCKT);
    	if (status == -NFS4ERR_DENIED)
    		return decode_lock_denied(xdr, &res->u.denied);
    	return status;